0

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

    }
}
Community
  • 1
  • 1
user3747260
  • 465
  • 1
  • 5
  • 14
  • I still don't see your byte array getting populated though. You are doing a POST which means you want to post some data somewhere. Where is that data? – Icemanind Jun 11 '15 at 21:51
  • I don't think there is any. I don't put any data in the javascript version either. The site seems to be designed so that a GET request returns a navigation error page but a POST returns the text for a json object which would then normally be used in a javascript script and then get put into an html table and displayed. So it needs a POST request, but in this case there's no data to send with it. I tried scraping the html, but ran into some issues with WebBrowser and when I noticed I could just grab the json directly, this seemed like it would be easier. – user3747260 Jun 11 '15 at 22:04

0 Answers0