c# – How to read a WebClient response after posting data? WebClient.UploadData Method (String, String, Byte[])

c# – How to read a WebClient response after posting data? WebClient.UploadData Method (String, String, Byte[])

Preferable way to send data in WebApi is a using JSON. But if you want to use form encoded data, you should:

  1. Hint WebAPI to use request body to read parameter and use simple type
    public HttpResponseMessage doc2pdf([FromBody]string fileContent)
  2. Send the data with a leading = sign.

So, client code

string uriString = http://www.Testcom;

WebClient myWebClient = new WebClient();
string postData = =data;
myWebClient.Headers.Add(Content-Type, application/x-www-form-urlencoded);
Console.WriteLine(myWebClient.Headers.ToString());
byte[] byteArray = Encoding.ASCII.GetBytes(postData);
byte[] responseArray = myWebClient.UploadData(new Uri(uriString), POST, byteArray);

Server side code

public HttpResponseMessage doc2pdf([FromBody]string fileContent)
{
    //..logic here
}

c# – How to read a WebClient response after posting data? WebClient.UploadData Method (String, String, Byte[])

Leave a Reply

Your email address will not be published.