public static async Task<string> GetData(string url, string data)
{
UriBuilder fullUri = new UriBuilder(url);
if (!string.IsNullOrEmpty(data))
fullUri.Query = data;
HttpClient client = new HttpClient();
HttpResponseMessage response = await client.PostAsync(new Uri(url), /*expects HttpContent*/);
response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
response.EnsureSuccessStatusCode();
string responseBody = await response.Content.ReadAsStringAsync();
return responseBody;
}
Run Code Online (Sandbox Code Playgroud)
将PostAsync采取一个需要另一个参数HttpContent.
我该如何设置HttpContent?任何适用于Windows Phone 8的文档都没有.
如果我这样做GetAsync,那就太棒了!但它需要POST的内容为key ="bla",某事="yay"
//编辑
非常感谢答案......这很有效,但这里还有一些不确定的地方:
public static async Task<string> GetData(string url, string data)
{
data = "test=something";
HttpClient client = new HttpClient();
StringContent queryString …Run Code Online (Sandbox Code Playgroud) 如何使用C#和HttpClient创建以下POST请求:

我需要这样的WEB API服务请求:
[ActionName("exist")]
[System.Web.Mvc.HttpPost]
public bool CheckIfUserExist([FromBody] string login)
{
bool result = _membershipProvider.CheckIfExist(login);
return result;
}
Run Code Online (Sandbox Code Playgroud) 我们有这个方法:
async Task<int> AccessTheWebAsync()
{
HttpClient client = new HttpClient();
Task<string> getStringTask = client.GetStringAsync("http://msdn.microsoft.com");
// You can do work here that doesn't rely on the string from GetStringAsync.
DoIndependentWork();
string urlContents = await getStringTask;
//The thing is that this returns an int to a method that has a return type of Task<int>
return urlContents.Length;
}
Run Code Online (Sandbox Code Playgroud)
之间是否隐式转换发生Task<int>和int?如果没有,那么发生了什么?它是如何实现的?
我有来自Nuget的这个 HttpClient.
当我想获取数据时,我这样做:
var response = await httpClient.GetAsync(url);
var data = await response.Content.ReadAsStringAsync();
Run Code Online (Sandbox Code Playgroud)
但问题是我不知道如何发布数据?我必须发送一个帖子请求并在其中发送这些值:comment="hello world"和questionId = 1.这些可以是一个类的属性,我不知道.
更新我不知道如何将这些值添加到HttpContentpost方法需要它.httClient.Post(string, HttpContent);
c# windows-phone-7 windows-phone dotnet-httpclient windows-phone-8
如何传入JSON有效内容以使用REST服务.
这是我正在尝试的:
var requestUrl = "http://example.org";
using (var client = new HttpClient())
{
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualifiedHeaderValue("application/json"));
var result = client.Post(requestUrl);
var content = result.Content.ReadAsString();
dynamic value = JsonValue.Parse(content);
string msg = String.Format("{0} {1}", value.SomeTest, value.AnotherTest);
return msg;
}
Run Code Online (Sandbox Code Playgroud)
如何将这样的内容作为参数传递给请求?:
{"SomeProp1":"abc","AnotherProp1":"123","NextProp2":"zyx"}
Run Code Online (Sandbox Code Playgroud) 在这里,我试图从服务中获取数据:
public async Task<IHttpActionResult> ValidateSesion()
{
var values = new Dictionary<string, string>{
{ "productId", "1" },
{ "productKey", "Abc6666" },
{ "userName", "OPPO" },
};
var json = JsonConvert.SerializeObject(values, Formatting.Indented);
// var content = new FormUrlEncodedContent(json);
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
var response = await client.PostAsync("http://172.116.12.172:8014/iadmin/validate", json);
var responseString = await response.Content.ReadAsStringAsync();
return Ok(responseString);
}
Run Code Online (Sandbox Code Playgroud)
如果我进行任何 PostMan 调用,如下所示,我得到的数据如下所示:
{
"productId": "1",
"productKey": "Abc6666"
}
Run Code Online (Sandbox Code Playgroud) c# ×6
json ×2
.net ×1
.net-4.5 ×1
asp.net ×1
asynchronous ×1
c#-5.0 ×1
httpclient ×1
httpcontent ×1
wcf ×1