【发布时间】:2014-06-08 18:35:10
【问题描述】:
我尝试用 C# 发出很长一段时间的 POST 请求,但它从来没有用过。
服务器的 statusCode 是 200,所以它似乎可以工作,但我得到的响应是“AccessDenied”。 HTTP 标头似乎是错误。
在 JS 上发出的 POST 请求效果很好,但在 C# 上却不行。
这是请求:
string resourceAddress = " ... ";
try
{
HttpClient httpClient = new HttpClient();
var content = new List<KeyValuePair <string,string>>
{
new KeyValuePair<string,string>(" ... "," ... "),
...
};
HttpResponseMessage response = await httpClient.PostAsync(resourceAddress, new FormUrlEncodedContent(content));
response.EnsureSuccessStatusCode();
var responseString = await response.Content.ReadAsStringAsync();
}
catch (HttpRequestException hre)
{
Debug.WriteLine(hre.ToString());
}
catch (Exception ex)
{
Debug.WriteLine(ex.ToString());
}
我错过了什么吗?
带有 C# 请求的 windows 8 是否修改或添加某些内容到标题中?
在 Android 上,我使用此代码:
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(urlWebSiteHTTPRequest);
try
{
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("... ","... "));
....
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
response = httpclient.execute(httppost);
entity = response.getEntity();
responseText = EntityUtils.toString(entity);
}
catch (ClientProtocolException e)
{
// TODO Auto-generated catch block
}
catch (IOException e)
{
// TODO Auto-generated catch block
}
【问题讨论】:
-
到底发生了什么?你有例外吗?
-
服务器是否需要认证?
-
你能提供 NetMon 或 Fiddler 的踪迹吗?
-
是的,服务器需要身份验证。我没有添加参数,但它们正在工作。我对其他操作系统使用相同的参数并且它正在工作。我忘了补充。我没有例外。我收到来自服务器的响应,但收到“AccessDenied”消息。我检查了清单上的必要参数,并添加了带有客户端和服务器的互联网。
-
很遗憾我不能使用 NetMon 或 Fiddler。
标签: c# windows windows-8 windows-8.1