C# 读取 http post 结果
C# Read http post result
我在将示例 guid 字符串放入 HttpResponseMessage 的 MVC 控制器中有以下代码:
public class CertifyController : Controller
{
[HttpPost]
public async Task<HttpResponseMessage> NewCertifyOfferRequestAsync(string requestString)
{
string activityId = "30dd879c-ee2f-11db-8314-0800200c9a66";
HttpResponseMessage httpResponseMessage = new HttpResponseMessage();
httpResponseMessage.Content = new StringContent(activityId);
return httpResponseMessage;
}
}
我正在使用以下代码通过控制台应用程序调用此控制器:
using (HttpClient client = new HttpClient())
{
client.BaseAddress = new Uri("http://localhost:84928/");
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
string requestString = "df5f5587-f1ef-449a-9d20-7f386ea638a8";
HttpResponseMessage response = await client.PostAsJsonAsync("Certify/NewCertifyOfferRequestAsync", requestString);
if (response.IsSuccessStatusCode)
{
string activityId = await response.Content.ReadAsStringAsync();
Console.WriteLine("Received activity id: " + activityId);
}
}
我希望在响应中收到 activityId“30dd879c-ee2f-11db-8314-0800200c9a66”。但是 activityId 从 ReadAsStringAsync 调用中获取 "StatusCode: 200, ReasonPhrase: 'OK', Version: 1.1, Content: System.Net.Http.StringContent, Headers:\r\n{\r\n Content-Type: text/plain; charset=utf-8\r\n}"。
我应该如何更改 activityId 的分配,以便它获得在 Controller 中生成的 activityId?
如果它是 webapi,您应该能够将方法签名更改为 return a "string" 而不是 HttpResponseMessage。
例如
public string Get(int id)
{
return "value";
}
有关详细示例,您可以使用 visual studio 创建 WebApi 网络应用程序,并查看 "ValuesController.cs"
我在将示例 guid 字符串放入 HttpResponseMessage 的 MVC 控制器中有以下代码:
public class CertifyController : Controller
{
[HttpPost]
public async Task<HttpResponseMessage> NewCertifyOfferRequestAsync(string requestString)
{
string activityId = "30dd879c-ee2f-11db-8314-0800200c9a66";
HttpResponseMessage httpResponseMessage = new HttpResponseMessage();
httpResponseMessage.Content = new StringContent(activityId);
return httpResponseMessage;
}
}
我正在使用以下代码通过控制台应用程序调用此控制器:
using (HttpClient client = new HttpClient())
{
client.BaseAddress = new Uri("http://localhost:84928/");
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
string requestString = "df5f5587-f1ef-449a-9d20-7f386ea638a8";
HttpResponseMessage response = await client.PostAsJsonAsync("Certify/NewCertifyOfferRequestAsync", requestString);
if (response.IsSuccessStatusCode)
{
string activityId = await response.Content.ReadAsStringAsync();
Console.WriteLine("Received activity id: " + activityId);
}
}
我希望在响应中收到 activityId“30dd879c-ee2f-11db-8314-0800200c9a66”。但是 activityId 从 ReadAsStringAsync 调用中获取 "StatusCode: 200, ReasonPhrase: 'OK', Version: 1.1, Content: System.Net.Http.StringContent, Headers:\r\n{\r\n Content-Type: text/plain; charset=utf-8\r\n}"。
我应该如何更改 activityId 的分配,以便它获得在 Controller 中生成的 activityId?
如果它是 webapi,您应该能够将方法签名更改为 return a "string" 而不是 HttpResponseMessage。
例如
public string Get(int id)
{
return "value";
}
有关详细示例,您可以使用 visual studio 创建 WebApi 网络应用程序,并查看 "ValuesController.cs"