POST and GET methods in C#

Asked

Viewed 1,756 times

1

I would like to know if you have any library or any third party component that do the POST and GET on the websites. I need to navigate some sites but I can not do GET nor on the home page 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 me this error message:

"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 every method of this link and all give the same error. https://stackoverflow.com/questions/4015324/http-request-with-post

I did the test by Postman (an extension of Chrome that performs these methods) and it worked normal.

  • 1

    You can put in your question your code attempts?

  • 1

    If the HttpClient does not work, the problem is not in the tool - it is in the way you use it. And unless you provide a MCVE, we can’t help you.

  • Attempts added @Gypsy

  • 1

    @lucasc01 It seems to me to be a specific server problem where it is picking up. There is nothing wrong with your code. But the server expects some form of request that does not occur using these classes. You need to figure out what is missing to meet the specific need. The page opens normally, these codes read other pages. The error is consistent in both ways. Maybe it waits for a user-agent or something that the browser usually provide by default. Maybe it’s purposeful to prevent people from automatically taking content.

1 answer

3


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

The browser, Curl and Postman use their own Useragent. Libraries don’t 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
  • It worked, thank you very much! @dcastro

Browser other questions tagged

You are not signed in. Login or sign up in order to post.