I'm trying to make a POST request from Unity using HttpWebRequest WebRequest, I get only HTML code while in Postman I get the right results.
I have tried using HttpWebRequest with StreamWriter, I tried doing it in normal way and async.
The function I use
public void GetResults()
{
byte[] bytes;
bytes = Encoding.ASCII.GetBytes("<?xml version=\"1.0\" encoding=\"UTF - 8\"?><blabla>asdasd</blabla> ");
string str = "[MY_API]";
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(str);
req.Method = "POST";
req.ContentType = "text/xml";
string strRequest = Encoding.ASCII.GetString(bytes);
req.ContentLength = strRequest.Length;
StreamWriter streamOut = new StreamWriter(req.GetRequestStream(), System.Text.Encoding.ASCII);
streamOut.Write(strRequest);
streamOut.Close();
StreamReader streamIn = new StreamReader(req.GetResponse().GetResponseStream());
string strResponse = streamIn.ReadToEnd();
print(strResponse);
streamIn.Close();
}
The expected print is XML code with some data, but actual print is
<html>
<body>
<h1>
200
</h1>
</body>
</html>
you need to specify the accept type:
req.Accept="application/xml" //or */*
I have used Unity3dAzure opensource awesome package, RESTClient It solved all my problems. Very easy use.
https://github.com/Unity3dAzure/RESTClient
Note: It looks like for XML what I'm doing is not enough, this package handles XMl and JSON in a good way.
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.