我尝试用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
}
答案 0 :(得分:0)
我认为标题格式不正确。尝试使用这个简单的工具。您将能够使用它来调试http请求消息,并实际查看服务器发回的数据和错误。 http://www.telerik.com/fiddler。转到composer选项卡以测试请求消息。还要确保您的授权用户名:密码是base64编码的。在大多数情况下,这似乎总是存在问题。
最好的运气。