简体   繁体   中英

How to get the content of a POST request?

I have a POST method in Web api that returns byte[].

[HttpPost]
[ActionName("adduser")]
public byte[] AddUser([NakedBody] byte[] data) { ... }

I make a reuest from mvc application to this method.

[HttpPost]
public ActionResult AddUser(RegistrationData data)
{
    byte[] requestPcmsMessage = CryptographyHelper.GetPcmsMessageFromModel(data);
    HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create("http://localhost:10189/portal/adduser");
    request.Method = "POST";
    request.KeepAlive = true;
    request.ContentLength = requestPcmsMessage.Length;
    using (var requestStream = request.GetRequestStream())
    {
        requestStream.Write(requestPcmsMessage, 0, requestPcmsMessage.Length);
    }
    HttpStatusCode statusCode;
    string responseString = "";
    using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
    {
        statusCode = response.StatusCode;

        if (statusCode == HttpStatusCode.OK)
        {
            responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();
        }
    }

    var responsePcmsMessage = CryptographyHelper.cryptoObject.ToBytes(responseString);

    ...    

    return View();
}

But the response I get in responsePcmsMessage is not the bytes I sent from server. So how can I get them?

I am not sure if this would be helpful but I see this website has mostly all the sample codes. They all have code attached to it at the end which is to do with getting back the response. So maybe you can try implementing it in similar manner?

Check some of the sample codes for Email Verification API . I really hope that helps you atleast a little.

public ActionResult AddUser([FromBody] RegistrationData data)

this is how I used it, using RestClient, i don't know if that works for you

// URL
string URL = "http://localhost:10189/portal/";
// client URL                 
var client = new RestClient(URL);
// what you want to do
var request = new RestRequest("adduser", Method.POST);
//Login-Data - if necessary
client.Authenticator = new HttpBasicAuthenticator("user", "password");
// the response you are looking for
IRestResponse response = client.Execute(request);
// return it to you
return response.Content;   

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM