2017-09-18 12 views
0

私はASP.NET Webサイトを持っています。ブラウザからURL「http://example.org/worktodo.ashx」を呼び出すと正常に動作します。HTTP/GET要求を送信中にC#WinApp throws(403)Fobidden例外

私は1つのアンドロイドアプリを作成しました。もし私がアンドロイドアプリから上記のURLを呼び出してもOKです。

私はC#でWindowsアプリケーションを作成しましたが、私は上記のWindowsアプリケーションから上記のURLを呼び出すとエラー403で失敗します。

以下はC#コードです。

 try 
     { 
      bool TEST_LOCAL = false; 
      // 
      // One way to call the url 
      // 
      WebClient client = new WebClient(); 
      string url = TEST_LOCAL ? "http://localhost:1805/webfolder/worktodo.ashx" : "http://example.org/worktodo.ashx"; 
      string status = client.DownloadString(url); 
      MessageBox.Show(status, "WebClient Response"); 

      // 
      // Another way to call the url 
      // 
      WebRequest request = WebRequest.Create(url); 
      request.Method = "GET"; 
      request.Headers.Add("Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8"); 
      request.Headers.Add("Connection:keep-alive"); 
      request.Headers.Add("User-Agent:Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.133 Safari/537.36"); 
      request.Headers.Add("Upgrade-Insecure-Requests:1"); 
      request.Headers.Add("Accept-Encoding:gzip, deflate, sdch"); 
      request.ContentType = "text/json"; 
      WebResponse response = request.GetResponse(); 

      string responseString = new System.IO.StreamReader(response.GetResponseStream()).ReadToEnd(); 
      MessageBox.Show(responseString, "WebRequest Response"); 
     } 
     catch (WebException ex) 
     { 
      string error = ex.Status.ToString(); 

     } 

スローされた例外がある:

 The remote server returned an error: (403) Forbidden. 
     StatusCode value is 'Forbidden' 
     StatusDescription value is 'ModSecurity Action' 

後は、(ライブラリを使用していますorg.apache.http)Androidアプリのコードです:解決するために、これまでに行わ

Handler handler = new Handler() { 
     Context ctx = context; // save context for use inside handleMessage() 
     @SuppressWarnings("deprecation") 
     public void handleMessage(Message message) { 
      switch (message.what) { 
       case HttpConnection.DID_START: { 
        break; 
       } 
       case HttpConnection.DID_SUCCEED: { 
        String response = (String) message.obj; 
        JSONObject jobjdata = null; 
        try { 
         JSONObject jobj = new JSONObject(response); 
         jobjdata = jobj.getJSONObject("data"); 
         String status = URLDecoder.decode(jobjdata.getString("status")); 
         Toast.makeText(ctx, status, Toast.LENGTH_LONG).show(); 
        } catch (Exception e1) { 
         Toast.makeText(ctx, "Unexpected error encountered", Toast.LENGTH_LONG).show(); 
         // e1.printStackTrace(); 
        } 
       } 
      } 
     } 
    }; 

    final ArrayList<NameValuePair> params1 = new ArrayList<NameValuePair>(); 
    if (RUN_LOCALLY) 
     new HttpConnection(handler).post(LOCAL_URL, params1); 
    else 
     new HttpConnection(handler).post(WEB_URL, params1); 
} 

取り組み/研究問題:

解決済みの解決策4 03禁断の彼らのためにエラーが、それは、私は、ファイル

  • 誰かのために「rwxの」アクセス許可を設定し、誰かが、ファイルには、適切な「rwxの」アクセス権が設定されている必要があると述べ

    1. 私の問題を解決しそうではありませんでした私が試したのは、USER-AGENTを指定したことです。もう一つの方法は、
    2. 誰かが有効なヘッダーがそれを固定し、言った)を呼び出すために - を設定することが有効なヘッダーを見つけるために、フィドラーを使用し、私はクローム/開発ツールと設定し、有効なヘッダ(REFを使用 別の方法を呼び出すために)
    3. が誰かに構成します。 ModSecurityを修正するにはModSecurityをインストールしていないので、私のWebサイトにはModSecurityがインストールされていません。
    4. 多くの人がMVCに問題を抱えていましたが、MVCを使用していません。私にとってではありません
    5. ModSecurityリファレンスマニュアルには、ウェブサイトからそれを削除するために、<modules><remove name="ModSecurityIIS" /></modules>をweb.configに追加しています。

      1. のAndroidアプリケーションが成功したとしてC#WinAppはどこ失敗した理由:私は私の質問がありましたが、問題を解決することができませんでした

  • なぜAndroidアプリケーションに 'ModSecurity Action'例外が発生しないのですか?
  • なぜC#WinAppで 'ModSecurity Action'例外が発生するのですか?
  • C#コードを修正するには?
  • 問題を解決するのを手伝ってください。皆さん、ありがとうございました。

    +0

    を必要としますがhttps://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Manually_Installing_and_Troubleshooting_Setup_of_ModSecurity_Module_on_IISを実行していますか? – mjwills

    +0

    私は自分のウェブサイトのためにホスティングを共有しています。私はそれを使用しませんでしたが、プロバイダがそれを使用したかどうかはわかりません。 ModSecurityが容疑者の場合、アンドロイドアプリもうまくいってはいけませんが、アンドロイドアプリは正常に動作しています。だから、私は本当に何がC#winappに間違っているのか理解できません。 – Rupesh

    +0

    上記のリンクで言及した製品を使用している場合は、ホスティングプロバイダにお問い合わせください。 – mjwills

    答えて

    0

    回答が見つかりました。以下は、期待どおりに動作するコードです。

    bool TEST_LOCAL = false; 
        string url = TEST_LOCAL ? "http://localhost:1805/webfolder/worktodo.ashx" : "http://example.org/worktodo.ashx"; 
    
        HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url); 
        request.Method = "GET"; 
        request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8"; 
        request.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.133 Safari/537.36"; 
        request.ContentType = "text/json"; 
    
        WebResponse response = request.GetResponse(); 
        string responseString = new System.IO.StreamReader(response.GetResponseStream()).ReadToEnd(); 
        MessageBox.Show(responseString, "WebRequest Response"); 
    

    注:using System.Net;

    関連する問題