简体   繁体   中英

Why I can't get request with HttpWebRequest?

I'd like to get phone number from https://sprzedajemy.pl/doskonale-dla-pary-planujacej-poszerzenie-rodziny-sprawdz-warszawa-2-1b8e55-nr57347155

Phone number is "protect" and I have to click "show number" to get request with phone. Before I send request I have to get from source data-id="805c74a74f3ea9fe6db5da90d722" from button "show number" and send POST with this token as _rp_offerID.

Correct answer is:

<span><strong><a href="tel:516000551"> 516 000 551</a></strong></span>

My answer is:

?

My complete code:

HttpWebRequest getRequest = (HttpWebRequest)WebRequest.Create("https://sprzedajemy.pl/oferta-dane.telefon");
        getRequest.Method = "POST";
        getRequest.UserAgent = "Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.116 Safari/537.36";
        getRequest.ContentType = "application/x-www-form-urlencoded";
        getRequest.Host = "sprzedajemy.pl";
        getRequest.Referer = url;
        getRequest.Headers.Add("accept-encoding", "gzip, deflate, br");
        getRequest.Headers.Add("accept-language", "pl,en-US;q=0.9,en;q=0.8,ru;q=0.7");
        getRequest.Headers.Add("origin", "https://sprzedajemy.pl");
        getRequest.Headers.Add("X-Requested-With", "XMLHttpRequest");

        var postData = "_rp_offerID=" + itemId;
        var data = Encoding.ASCII.GetBytes(postData);

        getRequest.ContentLength = data.Length;

        using (var stream = getRequest.GetRequestStream())
        {
            stream.Write(data, 0, data.Length);
        }

        var httpResponseP = (HttpWebResponse)getRequest.GetResponse();
        var streamReaderP = new StreamReader(httpResponseP.GetResponseStream());
        string strPhone = streamReaderP.ReadToEnd();

        Console.WriteLine(strPhone);

I don't know what is wrong with my code...

If I use REST client for Chrome with:

POST https://sprzedajemy.pl/oferta-dane.telefon User-Agent: Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.116 Safari/537.36 Content-Type: application/x-www-form-urlencoded Referer: http://sprzedajemy.pl/doskonale-dla-pary-planujacej-poszerzenie-rodziny-sprawdz-warszawa-2-1b8e55-nr57347155 accept-encoding: gzip, deflate, br accept-language: pl,en-US;q=0.9,en;q=0.8,ru;q=0.7 origin: https://sprzedajemy.pl X-Requested-With: XMLHttpRequest Host: sprzedajemy.pl Content-Length: 48

Body Form Data: _rp_offerID=80e158b0281e04a2102fd7bce6eba0cd3833

Why don't you use HttpClient ? it's much easier! check the example below:

using System;
using System.Net.Http;
using System.Text;

namespace httpClient
{
    class Program
    {

        static void Main(string[] args)
        {
            using (var client = new HttpClient() {BaseAddress = new Uri("https://sprzedajemy.pl")})
            {
                client.DefaultRequestHeaders.Add("accept-encoding", "gzip, deflate, br");
                client.DefaultRequestHeaders.Add("accept-language", "pl,en-US;q=0.9,en;q=0.8,ru;q=0.7");
                client.DefaultRequestHeaders.Add("origin", "https://sprzedajemy.pl");
                client.DefaultRequestHeaders.Add("X-Requested-With", "XMLHttpRequest");
                var postData = "_rp_offerID=80e158b0281e04a2102fd7bce6eba0cd3833";

                var stringContent = new StringContent(postData, Encoding.Default, "application/x-www-form-urlencoded");

                var result = client.PostAsync("oferta-dane.telefon", stringContent).GetAwaiter().GetResult();
            }
        }
    }
}

i`ve tested this code and the return was 200

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM