Web request works in .NET (visual studio) but not in Unity?

+4 votes
asked Jul 22, 2016 in Programming by Andy Mount Student (280 points)

I want to send a simple POST request to a webserver. In visual studio this flawlessly works. In Unity the server responses with 510 Not Extended. How could this be? It's just the same code.

            HttpWebRequest postRequest = (HttpWebRequest)WebRequest.Create(address);
            postRequest.CookieContainer = cookie;
            postRequest.Method = "POST";
            postRequest.KeepAlive = false;
            postRequest.ContentType = "application/x-www-form-urlencoded";
            postRequest.Accept = "text/html,application/xhtml+xml,application/xml";

            using (var writer = new StreamWriter(postRequest.GetRequestStream()))
            {
                writer.Write(parameters.ToString());
            }

Any suggestions on this?

 

 

commented Jul 22, 2016 by JD Student (300 points)
Seems like a Mono problem to me. What .NET version are you using in VS?
commented Jul 22, 2016 by Andy Mount Student (280 points)
I'm using .net 3.5.

2 Answers

+4 votes
answered Jul 22, 2016 by Bugstacker Senior (4,800 points)
selected Jul 22, 2016 by Andy Mount
 
Best answer

My guess is that Mono implemented web requests slightly different concerning redirections (302, 303). Try disabling the AllowAutoRedirect flag to prevent the system to resend the request as a GET request.

            HttpWebRequest postRequest = (HttpWebRequest)WebRequest.Create(address);
            postRequest.CookieContainer = cookie;
            postRequest.Method = "POST";
            postRequest.KeepAlive = false;
            postRequest.ContentType = "application/x-www-form-urlencoded";
            postRequest.Accept = "text/html,application/xhtml+xml,application/xml";
            postRequest.AllowAutoRedirect = false;
–1 vote
answered Jul 22, 2016 by Gleny Student (480 points)
I guess it's a webserver problem. Try disabling redirection on the server.
commented Jul 22, 2016 by Andy Mount Student (280 points)
but why does it work in vs?
Welcome to Bugstacker Q&A, where you can ask questions concerning Game Development, Programming and Design and receive answers from other members of the community.

Help for

...