为什么我无法通过 HttpWebRequest 获取请求?

Why I can't get request with HttpWebRequest?

我想从 https://sprzedajemy.pl/doskonale-dla-pary-planujacej-poszerzenie-rodziny-sprawdz-warszawa-2-1b8e55-nr57347155

获取 phone 号码

Phone 号码是 "protect",我必须单击 "show number" 才能获得 phone 的请求。在我发送请求之前,我必须从源数据 id="805c74a74f3ea9fe6db5da90d722" 从按钮 "show number" 获取并使用此令牌发送 POST 作为 _rp_offerID.

正确答案是:

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

我的回答是:

?

我的完整代码:

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

我不知道我的代码有什么问题...

如果我使用 Chrome 的 REST 客户端:

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

体型数据: _rp_offerID=80e158b0281e04a2102fd7bce6eba0cd3833

答案正确

你为什么不使用 HttpClient? 这更容易! 检查下面的例子:

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

我测试了这段代码 return 是 200