Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the content of HttpWebResponseMessage

I have a asp.net MVC razor C# application which has 1 controller and 1 POST function which accepts a parameter. And the function returns a 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;
    }
}

For testing purposes I've used Postman to do a POST request to the URL. It responds with:

StatusCode: 200, ReasonPhrase: 'OK', Version: 1.1, Content:      System.Net.Http.StringContent, Headers:
{
    Content-Type: text/plain; charset=utf-8
}

My statuscode reponds well, but I don't see my string "This is my output"

So I thought that maybe this is something to do with C# specific stuff, so I made a C# winforms application to test. So when I click the button it does the following:

    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);
        }            
    }
 }

This function works perfect but I still get the same reponse like the one in Postman... How can I get the actual content "This is my output"?

Edit

I've made another simple HttpGet request

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;
        }
     }
 }

While using Postman I get following result

StatusCode: 200, ReasonPhrase: 'OK', Version: 1.1, Content:     System.Net.Http.StringContent, Headers:
{
    Content-Type: text/plain; charset=utf-8
}
like image 907
koala Avatar asked Oct 26 '25 08:10

koala


1 Answers

You're mixing up WebAPI and MVC.

For WebAPI, the HttpResponseMessage (with Content = new StringContent("the string")) would work.

For MVC, the syntax to return a string is (note the ActionResult return type and Content() call):

public ActionResult Test() 
{
    return Content("This is my output");
}
like image 123
CodeCaster Avatar answered Oct 28 '25 20:10

CodeCaster



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!