【问题标题】:Send a POST request on Windows 8 metro App with C#使用 C# 在 Windows 8 Metro App 上发送 POST 请求
【发布时间】: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


【解决方案1】:

我认为标题格式不正确。尝试使用这个简单的工具。您将能够使用它来调试 http 请求消息并实际查看服务器发回的数据和错误。 http://www.telerik.com/fiddler。转到 composer 选项卡以测试请求消息。还要确保您的授权用户名:密码是 base64 编码的。在大多数情况下,这似乎总是问题所在。

祝你好运。

【讨论】:

  • OP 显然 stated 他不能使用 Fiddler
猜你喜欢
  • 2016-11-30
  • 2023-04-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-01-25
  • 2014-09-29
  • 1970-01-01
相关资源
最近更新 更多