尝试使用System.Net.Http.Post时发生C#错误消息
我正在开发Windows 8应用程序。错误消息“当我尝试执行代码时,名为'System.Net.Http'(缺少程序集引用?”)的类型或名称空间名称'Post'将出现在Visual Studio 2012中“尝试使用System.Net.Http.Post时发生C#错误消息
byte[] response = System.Net.Http.Post
(
url: "someurl",
contentType: "application/json",
contentLength: 32,
content: "pqpUserName=admin&[email protected]"
);
的代码是从URL .NET: Simplest way to send POST with data and read response
。任何帮助表示赞赏。
使用HttpClient:
var client = new HttpClient();
var pairs = new List<KeyValuePair<string, string>>
{
new KeyValuePair<string, string>("pqpUserName", "admin"),
new KeyValuePair<string, string>("password", "[email protected]")
};
var content = new FormUrlEncodedContent(pairs);
var response = client.PostAsync("yourURI", content).Result;
if (response.IsSuccessStatusCode)
{}
将System.Net.Http.HttpMethod
命名空间添加到您的代码中。
出现错误“不可调用的成员'System.Net.Http.HttpMethod.Post'不能像方法一样使用。” – Ramesh 2013-02-28 09:13:21
'HttpMethod'是一个枚举,而不是一个方法,所以是的,你得到这个错误是因为你没有正确使用它。 – 2013-02-28 09:45:19
这个代码是太旧了,利用新玩具:HttpClient的http://msdn.microsoft.com/en-us/library/system.net.http.httpclient。 aspx – 2013-02-28 07:26:06
它提供Post或等效功能吗? – Ramesh 2013-02-28 09:14:00
是的,其实--- --- – 2013-02-28 09:16:41