如何获取HttpWebResponseMessage的内容
How to get the content of HttpWebResponseMessage
我有一个 asp.net MVC razor C# 应用程序,它有 1 个控制器和 1 个接受参数的 POST 函数。函数 returns 一个 HttpResponseMessage.
public class VersionController : Controller
{
[HttpPost]
public HttpResponseMessage LatestClientVersion(string myVar)
{
string outputMessage = "This is my output";
...
var resp = new HttpResponseMessage(HttpStatusCode.OK);
resp.Content = new StringContent(outputMessage, Encoding.UTF8, "text/plain");
return resp;
}
}
出于测试目的,我使用 Postman 向 URL 发出了 POST 请求。
它响应:
StatusCode: 200, ReasonPhrase: 'OK', Version: 1.1, Content: System.Net.Http.StringContent, Headers:
{
Content-Type: text/plain; charset=utf-8
}
我的状态码响应良好,但我没有看到我的字符串 "This is my output"
所以我认为这可能与 C# 特定的东西有关,所以我制作了一个 C# winforms 应用程序来测试。因此,当我单击按钮时,它会执行以下操作:
public void TestWebRequest()
{
try
{
WebRequest request = WebRequest.Create(txtURL.Text);
request.Method = "POST";
string postData = "myVar=test";
byte[] byteArray = Encoding.UTF8.GetBytes(postData);
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = byteArray.Length;
Stream dataStream = request.GetRequestStream();
dataStream.Write(byteArray, 0, byteArray.Length);
dataStream.Close();
WebResponse response = request.GetResponse();
HttpWebResponse httpResponse = (HttpWebResponse)request.GetResponse();
dataStream = response.GetResponseStream();
StreamReader reader = new StreamReader(dataStream);
string responseFromServer = reader.ReadToEnd();
// Send text to the textbox
SetOutputText(responseFromServer);
reader.Close();
dataStream.Close();
response.Close();
}
catch (Exception ex)
{
SetOutputText(ex.Message);
}
}
}
此功能完美运行,但我仍然得到与 Postman 中相同的响应...
如何获取实际内容"This is my output"?
编辑
我发出了另一个简单的 HttpGet 请求
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Web;
using System.Web.Mvc;
using System.Web.Configuration;
using System.Net.Http.Headers;
using System.Net;
using System.Text;
namespace MyWebService.Controllers
{
public class VersionController : Controller
{
[HttpGet]
public HttpResponseMessage Test()
{
HttpResponseMessage response = new HttpResponseMessage();
response.Content = new StringContent("This is my output");
return response;
}
}
}
在使用 Postman 时我得到以下结果
StatusCode: 200, ReasonPhrase: 'OK', Version: 1.1, Content: System.Net.Http.StringContent, Headers:
{
Content-Type: text/plain; charset=utf-8
}
您混淆了 WebAPI 和 MVC。
对于 WebAPI,HttpResponseMessage
(带 Content = new StringContent("the string")
)可以工作。
对于 MVC,syntax to return a string is(注意 ActionResult
return 类型和 Content()
调用):
public ActionResult Test()
{
return Content("This is my output");
}
我有一个 asp.net MVC razor C# 应用程序,它有 1 个控制器和 1 个接受参数的 POST 函数。函数 returns 一个 HttpResponseMessage.
public class VersionController : Controller
{
[HttpPost]
public HttpResponseMessage LatestClientVersion(string myVar)
{
string outputMessage = "This is my output";
...
var resp = new HttpResponseMessage(HttpStatusCode.OK);
resp.Content = new StringContent(outputMessage, Encoding.UTF8, "text/plain");
return resp;
}
}
出于测试目的,我使用 Postman 向 URL 发出了 POST 请求。 它响应:
StatusCode: 200, ReasonPhrase: 'OK', Version: 1.1, Content: System.Net.Http.StringContent, Headers:
{
Content-Type: text/plain; charset=utf-8
}
我的状态码响应良好,但我没有看到我的字符串 "This is my output"
所以我认为这可能与 C# 特定的东西有关,所以我制作了一个 C# winforms 应用程序来测试。因此,当我单击按钮时,它会执行以下操作:
public void TestWebRequest()
{
try
{
WebRequest request = WebRequest.Create(txtURL.Text);
request.Method = "POST";
string postData = "myVar=test";
byte[] byteArray = Encoding.UTF8.GetBytes(postData);
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = byteArray.Length;
Stream dataStream = request.GetRequestStream();
dataStream.Write(byteArray, 0, byteArray.Length);
dataStream.Close();
WebResponse response = request.GetResponse();
HttpWebResponse httpResponse = (HttpWebResponse)request.GetResponse();
dataStream = response.GetResponseStream();
StreamReader reader = new StreamReader(dataStream);
string responseFromServer = reader.ReadToEnd();
// Send text to the textbox
SetOutputText(responseFromServer);
reader.Close();
dataStream.Close();
response.Close();
}
catch (Exception ex)
{
SetOutputText(ex.Message);
}
}
}
此功能完美运行,但我仍然得到与 Postman 中相同的响应... 如何获取实际内容"This is my output"?
编辑
我发出了另一个简单的 HttpGet 请求
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Web;
using System.Web.Mvc;
using System.Web.Configuration;
using System.Net.Http.Headers;
using System.Net;
using System.Text;
namespace MyWebService.Controllers
{
public class VersionController : Controller
{
[HttpGet]
public HttpResponseMessage Test()
{
HttpResponseMessage response = new HttpResponseMessage();
response.Content = new StringContent("This is my output");
return response;
}
}
}
在使用 Postman 时我得到以下结果
StatusCode: 200, ReasonPhrase: 'OK', Version: 1.1, Content: System.Net.Http.StringContent, Headers:
{
Content-Type: text/plain; charset=utf-8
}
您混淆了 WebAPI 和 MVC。
对于 WebAPI,HttpResponseMessage
(带 Content = new StringContent("the string")
)可以工作。
对于 MVC,syntax to return a string is(注意 ActionResult
return 类型和 Content()
调用):
public ActionResult Test()
{
return Content("This is my output");
}