Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 months ago.
Improve this question
I have the following GET request in python:
url = "https://web.trf3.jus.br/certidao/Certidao/Solicitar"
payload={}
headers = {
'authority': 'web.trf3.jus.br',
'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',
'accept-language': 'pt-BR,pt-PT;q=0.9,pt;q=0.8,en-US;q=0.7,en;q=0.6',
'cache-control': 'max-age=0',
'sec-ch-ua': '"Chromium";v="104", " Not A;Brand";v="99", "Google Chrome";v="104"',
'sec-ch-ua-mobile': '?0',
'sec-ch-ua-platform': '"Windows"',
'sec-fetch-dest': 'document',
'sec-fetch-mode': 'navigate',
'sec-fetch-site': 'none',
'sec-fetch-user': '?1',
'upgrade-insecure-requests': '1',
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.0.0 Safari/537.36'
}
response = requests.request("GET", url, headers=headers, data=payload)
It works fine. However, when I try to do the same request in C# using RestSharp, I always get timeout errors. The request never completes successfully.
var request = new RestRequest("https://web.trf3.jus.br/certidao/Certidao/Solicitar", Method.Get);
request.AddHeader("authority", "web.trf3.jus.br");
request.AddHeader("accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9");
request.AddHeader("accept-language", "pt-BR,pt-PT;q=0.9,pt;q=0.8,en-US;q=0.7,en;q=0.6");
request.AddHeader("cache-control", "max-age=0");
request.AddHeader("sec-ch-ua", "\"Chromium\";v=\"104\", \" Not A;Brand\";v=\"99\", \"Google Chrome\";v=\"104\"");
request.AddHeader("sec-ch-ua-mobile", "?0");
request.AddHeader("sec-ch-ua-platform", "\"Windows\"");
request.AddHeader("sec-fetch-dest", "document");
request.AddHeader("sec-fetch-mode", "navigate");
request.AddHeader("sec-fetch-site", "none");
request.AddHeader("sec-fetch-user", "?1");
request.AddHeader("upgrade-insecure-requests", "1");
request.AddHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.0.0 Safari/537.36");
While it does not answer the question why, the following works well using Flurl, which is my personal preference for http calls:
using Flurl.Http;
namespace Program
{
public static class Program
{
static async Task Main()
{
var url = "https://web.trf3.jus.br/certidao/Certidao/Solicitar";
var headers = new Dictionary<string, string>();
headers.Add("authority", "web.trf3.jus.br");
headers.Add("accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9");
headers.Add("accept-language", "pt-BR,pt-PT;q=0.9,pt;q=0.8,en-US;q=0.7,en;q=0.6");
headers.Add("cache-control", "max-age=0");
headers.Add("sec-ch-ua", "\"Chromium\";v=\"104\", \" Not A;Brand\";v=\"99\", \"Google Chrome\";v=\"104\"");
headers.Add("sec-ch-ua-mobile", "?0");
headers.Add("sec-ch-ua-platform", "\"Windows\"");
headers.Add("sec-fetch-dest", "document");
headers.Add("sec-fetch-mode", "navigate");
headers.Add("sec-fetch-site", "none");
headers.Add("sec-fetch-user", "?1");
headers.Add("upgrade-insecure-requests", "1");
headers.Add("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.0.0 Safari/537.36");
var result = await url.WithHeaders(headers).GetStringAsync();
}
}
}
This works too using RestSharp (have you forgotten await by any chance?)
using Flurl.Http;
using RestSharp;
namespace Program
{
public static class Program
{
static async Task Main()
{
var url = "https://web.trf3.jus.br/certidao/Certidao/Solicitar";
var headers = new Dictionary<string, string>
{
{ "authority", "web.trf3.jus.br" },
{ "accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9" },
{ "accept-language", "pt-BR,pt-PT;q=0.9,pt;q=0.8,en-US;q=0.7,en;q=0.6" },
{ "cache-control", "max-age=0" },
{ "sec-ch-ua", "\"Chromium\";v=\"104\", \" Not A;Brand\";v=\"99\", \"Google Chrome\";v=\"104\"" },
{ "sec-ch-ua-mobile", "?0" },
{ "sec-ch-ua-platform", "\"Windows\"" },
{ "sec-fetch-dest", "document" },
{ "sec-fetch-mode", "navigate" },
{ "sec-fetch-site", "none" },
{ "sec-fetch-user", "?1" },
{ "upgrade-insecure-requests", "1" },
{ "User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.0.0 Safari/537.36" }
};
var client = new RestClient();
var request = new RestRequest(url,Method.Get);
request.AddHeaders(headers);
var result = await client.ExecuteAsync(request);
}
}
}
Related
How can I download OSM tiles? I specified various request headers, but the site always returns 0 bytes.
WebClient client = new WebClient())
client.Headers.Add("method", "GET");
client.Headers.Add("scheme", "https");
client.Headers.Add(HttpRequestHeader.AcceptLanguage, "en-US");
client.Headers.Add("sec-ch-ua", "\"Chromium\";v=\"88\", \"Google Chrome\";v=\"88\", \"; Not A Brand\";v=\"99\"");
client.Headers.Add(HttpRequestHeader.Referer, "https://www.openstreetmap.org/");
client.Headers.Add("sec-fetch-dest", "document");
client.Headers.Add("sec-fetch-mode", "navigate");
client.Headers.Add("sec-fetch-site", "same-site");
client.Headers.Add("sec-fetch-user", "?1");
client.Headers.Add("upgrade-insecure-requests", "1");
client.Headers.Add(HttpRequestHeader.UserAgent, "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.190 Safari/537.36");
client.DownloadData("https://tile.openstreetmap.org/0/0/0.png");
UPD:
client.Headers.Add("sec-ch-ua", "\" Not A; Brand\";v=\"99\", \"Chromium\";v=\"101\", \"Microsoft Edge\";v=\"101\"");
client.Headers.Add(HttpRequestHeader.UserAgent, "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/101.0.4951.64 Safari/537.36 Edg/101.0.1210.47");
These values from my browser don't work either.
So Im learning RestSharp
But I'm stuck at this problem which is getting specific string for client cookies here is my code:
var cookieJar = new CookieContainer();
var client = new RestClient("https://server.com")
{
UserAgent =
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36",
};
client.CookieContainer = cookieJar;
var request = new RestRequest(Method.GET);
var cookie = client.CookieContainer.GetCookieHeader(new Uri("https://server.com"));
MessageBox.Show(""+cookie);
and I always get the cookie empty can anyone helps me?
This will set the cookie for your client. After all, you need to do is client.Execute. The code is in C# pretty sure you can make it work for anything else.
string myUserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36";
client.CookieContainer.Add(new Cookie("UserAgent", myUserAgent) { Domain = myUri.Host });
It is impossible to register. Writes:
{\"bSuccess\":false,\"details\":\"captcha data missing!\"}
I checked all the CAPTCHA correctly. The request also seems true. What's wrong?
PS I using xNet
public bool accaunt_create(AccauntData ad)
{
HttpRequest httpRequest = new HttpRequest();
httpRequest.AllowAutoRedirect = true;
httpRequest.UserAgent = "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.85 Safari/537.36 OPR/32.0.1948.38";
httpRequest.Cookies = cook;
httpRequest.AddParam("accountname", ad.accountname);
httpRequest.AddParam("password", ad.password);
httpRequest.AddParam("email", ad.email);
httpRequest.AddParam("captchagid", captchaID);
httpRequest.AddHeader("captcha_text", ad.captcha_text);
httpRequest.AddParam("i_agree", "1");
httpRequest.AddParam("ticket");
httpRequest.AddParam("count", "4");
httpRequest.AddHeader("X-Requested-With", "XMLHttpRequest");
httpRequest.AddHeader("X-Prototype-Version", "1.7");
httpRequest.AddHeader("Accept", "text/javascript, text/html, application/xml, text/xml, */*");
var res = httpRequest.Post("https://store.steampowered.com/join/createaccount/").ToString();
return false;
}
maybe this
httpRequest.AddParam("captchagid", captchaID);
have to be this:
httpRequest.AddParam("captchaid", captchaID);
POST DATA =>
Accept:application/json, text/javascript, */*; q=0.01
Accept-Charset:ISO-8859-9,utf-8;q=0.7,*;q=0.3
Accept-Encoding:gzip,deflate,sdch
Accept-Language:tr-TR,tr;q=0.8,en-US;q=0.6,en;q=0.4
Connection:keep-alive
Content-Length:0
Cookie:pfu=32904422; pfp=PO7PkdBDUwKoMG4FqkriwDLF7jrwcHBEoVqnX2i3; pfe=1386687638;
logged_in=1; tmgioct=5hRBmncU3JQtInFOSa4qqoHX
Host:www.tumblr.com
Origin:http://www.tumblr.com
Referer:http://www.tumblr.com/customize/hayirasla?redirect_to=http%3A%2F%2Fhayirasla.tumblr.com%2F
User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.95 Safari/537.11
X-Requested-With:XMLHttpRequest
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "POST";
request.Headers.Add("Origin", "http://www.tumblr.com");
request.Headers.Add(HttpRequestHeader.UserAgent, "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.95 Safari/537.11"); //Bu üstbilgi uygun özellik veya yöntem kullanılarak değiştirilmelidir.
request.Headers.Add(HttpRequestHeader.Cookie, "pfu=32904422;pfe=1386687638;pfp=PO7PkdBDUwKoMG4FqkriwDLF7jrwcHBEoVqnX2i3;logged_in=1;");
return new StreamReader(((HttpWebResponse)request.GetResponse()).GetResponseStream()).ReadToEnd();
you cannot add headers via HttpWebRequest.Headers.Add()
certain headers are set to be restricted
private static readonly string[] RestrictedHeaders = new[]
{
"Accept", "Connection", "Content-Type", "Content-Length", "Date", "Expect", "Host", "Range", "Referer", "User-Agent"
};
all the headers listed above are not to be added directly, they can only be set by using
//HttpWebRequest.[TheProperty] = value;
You forgot to "fill" the post-data?!? for what i see you're sending a request that only has headers...
The question is how to construct HttpWebRequest so queried server will think it comes from a browser?
You could set the User-Agent HTTP request header.
var request = (HttpWebRequest)WebRequest.Create("http://www.google.com");
request.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.2 (KHTML, like Gecko) Chrome/15.0.874.121 Safari/535.2";
or if you work with a WebClient:
using (var client = new WebClient())
{
client.Headers[HttpRequestHeader.UserAgent] = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.2 (KHTML, like Gecko) Chrome/15.0.874.121 Safari/535.2";
...
}