Basic POST HttpWebRequest fails to send, but works in browser

620 views Asked by At

Edit: I also tried using HttpRequest and ran into a similar error, but I have also found now that it works with curl. I wonder if there is something wrong with some configuration in my .NET? I wouldn't know where to even begin looking though.

I am trying to make a very simple POST, but I receive the WebException

The underlying connection was closed: An unexpected error occurred on a send.

I'm not very familiar with this kind of work and I'm out of ideas, so any suggestions would be appreciated.

I have added all the headers, and tried adjusting ServicePointManager.MaxServicePointIdleTime and KeepAlive. This particular request has an empty body as it should, but I have also tried requests with bodies that aren't empty to other URLs of the same site.

I can successfully send the POST with a browser using the method demonstrated here: https://stackoverflow.com/a/11886281/3747260

var formPost = document.createElement('form');
formPost.method = 'POST';
formPost.action = 'https://vulcun.com/api/games';
document.body.appendChild(formPost);
formPost.submit();

And here is how I'm trying to send the HttpWebRequest using this template(I started with the basics and then just kept adding headers that were in the browser request): https://msdn.microsoft.com/en-us/library/debx8sh9(v=vs.110).aspx

using System;
using System.Net;
using System.IO;
namespace Scraper{
    class MainClass{
        public static void Main (string[] args){
            try{
                //ServicePointManager.MaxServicePointIdleTime = 15;
                HttpWebRequest rq = (HttpWebRequest)WebRequest.Create("https://vulcun.com/api/games");
                //rq.KeepAlive = false;
                byte[] byteArray = { };
                rq.Host = "vulcun.com";
                rq.Method = "POST";
                rq.ContentLength = byteArray.Length;
                rq.ContentType = "application/x-www-form-urlencoded";
                rq.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
                rq.Headers.Add("Accept-Encoding","gzip, deflate");
                rq.Headers.Add("Accept-Language", "en-US,en;q=0.5");


                Stream dataStream = rq.GetRequestStream();
                dataStream.Write(byteArray, 0, byteArray.Length);
                dataStream.Close();

                HttpWebResponse resp = (HttpWebResponse)rq.GetResponse();


            }
            catch(WebException e){
                Console.WriteLine("Message: " + e.Message);
                if (e.Status == WebExceptionStatus.ProtocolError){
                    Console.WriteLine("Status Code: {0}", ((HttpWebResponse)e.Response).StatusCode);
                    Console.WriteLine("Status Description : {0}", ((HttpWebResponse)e.Response).StatusDescription);
                }
            }
            catch(Exception e){
                Console.WriteLine(e.Message);
            }

            Console.ReadLine();
        }

    }
}
0

There are 0 answers