POST and GET methods in C #

1

I wonder if you have any library or any third-party component that does POST and GET on the sites. I need to navigate to some sites but I can not do GET nor the homepage of a specific site, it always returns me error 599.

Attempts:

 private void button8_Click(object sender, EventArgs e)
    {
        WebClient client = new WebClient();

        string html = client.DownloadString("http://aplicacao2.jt.jus.br/dejt/");
    }

private void button9_Click(object sender, EventArgs e)
    {
        var request = (HttpWebRequest)WebRequest.Create("http://aplicacao2.jt.jus.br/dejt");

        var response = (HttpWebResponse)request.GetResponse();

        string html = new StreamReader(response.GetResponseStream()).ReadToEnd();
    }

The two attempts return this error message to me:

  

"An unhandled exception of type 'System.Net.WebException' occurred in   System.dll

     

Additional information: The remote server returned an error: (599)   Unknown Reason. "

I have tried all the methods of this link and everyone gives the same error. link

I did the test for Postman (a chrome extension that performs these methods) and it worked fine.

    
asked by anonymous 28.08.2015 / 19:59

1 answer

3

It seems like this site requires the presence of the User-Agent header. So just choose one (I used the Curl user agent in the example below).

The browser, Curl, and Postman use their own UserAgent. Libraries do not use any - that's why your attempts were failing.

public async Task Method()
{
    var client = new HttpClient
    {
        DefaultRequestHeaders =
        {
            {"User-Agent", "curl/7.30.0"},
        }
    };

    var response = await client.GetAsync("http://aplicacao2.jt.jus.br/dejt/");

    var content = await response.Content.ReadAsStringAsync();
}

Here is an excerpt from the command curl http://aplicacao2.jt.jus.br/dejt/ -vIX GET , for comparison:

* Adding handle: conn: 0xd6e590
* Adding handle: send: 0
* Adding handle: recv: 0
* Curl_addHandleToPipeline: length: 1
* - Conn 0 (0xd6e590) send_pipe: 1, recv_pipe: 0
* About to connect() to aplicacao2.jt.jus.br port 80 (#0)
*   Trying 201.49.155.48...
* Connected to aplicacao2.jt.jus.br (201.49.155.48) port 80 (#0)
> GET /dejt/ HTTP/1.1
> User-Agent: curl/7.30.0
> Host: aplicacao2.jt.jus.br
> Accept: */*
>
< HTTP/1.1 200 OK
HTTP/1.1 200 OK
    
28.08.2015 / 20:41