【发布时间】:2017-09-18 11:42:33
【问题描述】:
我有 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 / 开发人员工具并设置有效标头(参考。 另一种调用方式)
- 有人配置了 ModSecurity 来修复它,但是,我没有为我的网站安装 ModSecurity,所以不适合我
- 许多人在使用 MVC 时遇到问题并修复了它,但是,我不使用 MVC,所以这些解决方案不适合我
- ModSecurity 参考手册说,要将其从网站中删除,请将
<modules><remove name="ModSecurityIIS" /></modules>添加到 web.config。我做了,但无法解决问题
我的问题是:
- 为什么 C# WinApp 失败,而 Android App 成功?
- 为什么 Android 应用没有遇到“ModSecurity Action”异常?
- 为什么 C# WinApp 遇到“ModSecurity Action”异常?
- 如何修复 C# 代码?
请帮我解决问题。谢谢大家。
【问题讨论】:
-
我已为我的网站共享主机。我没有使用它,但我不知道提供者是否使用它。如果 ModSecurity 是嫌疑人,那么 android 应用程序一定不能正常工作,但该 android 应用程序工作正常。所以,我真的不明白 C# winapp 出了什么问题。
-
如果他们正在使用我在上面链接中提到的产品,请询问您的托管服务提供商。
标签: c# http-get winforms-to-web