.Net c# Webrequest Post

Asked

Viewed 419 times

0

People I’m studying Webrequest, I’m trying to make a query on the site of the post office and so I read it should look like this:

WebRequest request = WebRequest.Create("http://www2.correios.com.br/sistemas/rastreamento/resultado.cfm");
        request.Method = WebRequestMethods.Http.Post;
        request.ContentType = "application/x-www-form-urlencoded";
        request.Credentials = CredentialCache.DefaultCredentials;

        using (var stream = request.GetRequestStream())
        {
            var buffer = Encoding.UTF8.GetBytes("objetos=PU633524761BR");
            stream.Write(buffer, 0, buffer.Length);
        }

        HttpWebResponse response = (HttpWebResponse)request.GetResponse();
        Console.WriteLine(response.StatusDescription);
        Stream dataStream = response.GetResponseStream();
        StreamReader reader = new StreamReader(dataStream);
        string responseFromServer = reader.ReadToEnd();
        textBox.Text = responseFromServer;
        Console.WriteLine(responseFromServer);
        reader.Close();
        dataStream.Close();
        response.Close();

however when running it is not showing the posting history of the object so that I can retrieve the information

  • I’m pretty sure that’s not how you consume the mail tracking API. You’ve read the same documentation?

  • yes, it’s not even, I’m just studying the issue of Webrequest, to consume the API of the post office is totally different

1 answer

0


I believe that you have confused with the order of the requests made by the page and that is why you are not emulating it correctly. Analyzing realized that she first makes a request on another page and then redirects to which you are trying to make the call (which will not work because as comes by cookies you will not have anything). The correct is more or less like this:

var request = (HttpWebRequest)WebRequest.Create("https://www2.correios.com.br/sistemas/rastreamento/ctrl/ctrlRastreamento.cfm?");  //alterado para o lugar correto do post, fiz um cast pra httpwebrequest para poder usar o cookiecontainer e assim tê-los na próxima página.
            request.Method = WebRequestMethods.Http.Post;
            var cookies = new CookieContainer();  //incluido cookie container
            request.CookieContainer = cookies;  //atribuido a requisição
            request.ContentType = "application/x-www-form-urlencoded";
            request.Credentials = CredentialCache.DefaultCredentials;

            using (var stream = request.GetRequestStream())
            {
                var buffer = Encoding.UTF8.GetBytes("objetos=PU633524761BR");
                stream.Write(buffer, 0, buffer.Length);
            }

            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            Console.WriteLine(response.StatusDescription);
            Stream dataStream = response.GetResponseStream();
            StreamReader reader = new StreamReader(dataStream);
            string responseFromServer = reader.ReadToEnd();
            Console.WriteLine(responseFromServer);
            reader.Close();
            dataStream.Close();
            response.Close();
  • actually what was wrong was HTTPS and not HTTP

Browser other questions tagged

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