我想从 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
答案是正确的
为什么不使用 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