我有 ASP.NET 网站。当我从浏览器调用 url ' http://example.org/worktodo.ashx ' 时,它工作正常。
我已经创建了一个 android 应用程序,如果我从 android 应用程序调用上面的 url,那么它也可以正常工作。
我已经在 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'
以下是 android 应用程序代码(使用 org.apache.http 库):
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);
}
迄今为止为解决该问题所做的努力/研究:
我发现以下解决方案为他们修复了 403 禁止错误,但无法解决我的问题
- 有人说,文件需要设置适当的 'rwx' 权限,所以,我为文件设置了 'rwx' 权限
- 有人说,指定 USER-AGENT 有效,我试过(参考另一种调用方式)
- 有人说,有效的标头修复了它 - 使用 Fiddler 找到要设置的有效标头,我使用 Chrome / Developer Tools 并设置了有效的标头(参考另一种调用方式)
- 有人配置了 ModSecurity 来修复它,但是,我没有为我的网站安装 ModSecurity,所以对我来说不是一个选项
- 许多人遇到了 MVC 问题并修复了它,但是,我不使用 MVC,所以这些解决方案不适合我
- ModSecurity 参考手册说,要将其从网站中删除,请添加
<modules><remove name="ModSecurityIIS" /></modules>
到 web.config。我做了但无法解决问题
我的问题是:
- 为什么 C# WinApp 在 Android App 成功的地方失败了?
- 为什么 Android 应用不会遇到“ModSecurity Action”异常?
- 为什么 C# WinApp 遇到“ModSecurity Action”异常?
- 如何修复 C# 代码?
请帮我解决问题。谢谢你们。