如何获取HttpWebResponseMessage的内容

时间:2015-03-30 11:15:02

标签: c# asp.net web-services razor

我有一个asp.net MVC razor C#应用程序,它有1个控制器和1个接受参数的POST函数。该函数返回一个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
}

我的状态代码响应很好,但我没有看到我的字符串"这是我的输出"

所以我认为这可能与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);
        }            
    }
 }

这个功能很完美,但我仍然得到像邮递员那样的响应...... 如何获得实际内容"这是我的输出"?

修改

我做了另一个简单的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
}

1 个答案:

答案 0 :(得分:4)

你正在混淆WebAPI和MVC。

对于Web API,HttpResponseMessageContent = new StringContent("the string"))可以使用。

对于MVC,syntax to return a string is(请注意ActionResult返回类型和Content()来电):

public ActionResult Test() 
{
    return Content("This is my output");
}