如何获取POST请求的内容?

时间:2016-06-27 12:16:25

标签: c# asp.net-mvc asp.net-web-api

我在Web api中有一个返回byte []的POST方法。

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

我从mvc应用程序到这个方法做了一个回应。

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

但是我在responsePcmsMessage中得到的响应不是我从服务器发送的字节。那我怎么能得到它们呢?

3 个答案:

答案 0 :(得分:0)

我不确定这是否有用但我看到这个网站主要包含所有示例代码。它们都在最后附加了代码,这与回复响应有关。那么也许您可以尝试以类似的方式实现它?

检查Email Verification API的部分示例代码。我真的希望能帮到你一点点。

答案 1 :(得分:0)

public ActionResult AddUser([FromBody] RegistrationData data)

答案 2 :(得分:0)

这是我使用它的方式,使用RestClient,我不知道这是否适合你

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