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

时间:2018-12-26 14:44:06

标签: c# web-scraping httpwebrequest webclient

我想从https://sprzedajemy.pl/doskonale-dla-pary-planujacej-poszerzenie-rodziny-sprawdz-warszawa-2-1b8e55-nr57347155获得电话号码

电话号码是“保护”,我必须单击“显示号码”以获取电话请求。在发送请求之前,我必须从“显示编号”按钮中获取源data-id =“ 805c74a74f3ea9fe6db5da90d722”并发送带有此令牌的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);

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

如果我将REST客户端与Chrome配合使用:

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

答案正确

1 个答案:

答案 0 :(得分:1)

为什么不使用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();
            }
        }
    }
}

我已经测试了这段代码,返回的是200