C# RestSharp Library AddHeader Method not work - c#

Here is my code:
var cclient = new RestClient("ex.com");
var reqq = new RestRequest("ex", Method.POST);
reqq.AddHeader("Cookie", coockie);
reqq.AddHeader("Host", "profile.callofduty.com");
reqq.AddHeader("Pragma", "no-cache");
reqq.AddHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; rv:11.0) like Gecko");
reqq.AddParameter("username", log);
reqq.AddParameter("password", pas);
var responsee = cclient.Post(reqq);
but on http debugger when I inspect the request:
I Don't now the problem please help. none of headers submited

AddHeader(key, value) method is a "proxy" for AddParameter(key, value, ParameterType.HttpHeader) method. All your headers are applied to the request and can be found using ordinary debugger if you inspect Parameters property of your RestRequest instance (in your code sample it's reqq).
Also to send your request you really should use the Execute(IRestRequest) method in your RestClient

Related

Received an invalid header name: '#<FCGI'. in c# when I try to get data from a server

I'm trying to connect from a C# app to a web server and I want to get values from it and save it to a data base. This server has a login, so I'm trying to replicate the browser behaviour, when I send the login request, I get an exception System.Net.Requests with the mesage: Received an invalid header name: '#<FCGI'.
I've tried to add the header with the sentence: request.Headers.Add("#<FCGI:");, but I get a new System.Net.WebHeaderCollection exception, with the following message: Specified value has invalid HTTP Header characters. (Parameter 'name').
The code I'm using is the following:
HttpWebRequest request = (HttpWebRequest)WebRequest.CreateHttp($"http://{this.ipaddr}/app/app.app?cmd=authority_login&username=user&userpass=password");
request.KeepAlive = true;
request.ContentType="application/json";
request.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:88.0) Gecko/20100101 Firefox/88.0";
request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8";
request.Method = "GET";
request.Headers.Add("#<FCGI:");
WebResponse response1 = request.GetResponse();
I captured the trafic with the server with
Wireshark (picture here), and the full header is
#<FCGI: :CGI:0x405b5068 #output_hidden=nil, #cookies={"sid"=>["ad7ee82e6731416ef7f1d0e80b266906"], "dispzone"=>["client"], "lang"=>["en"]}, #args=nil, #multipart=false, #output_cookies=nil, #params={"username"=>["User"], "cmd"=>["authority_login"], "userpass"=>["espec"]}, #request=#<FCGI::Request:0x405b5230 #id=1, #out=#<StringIO:0x405b50e0>, #in=#<StringIO:0x405b5170>, #data=#<StringIO:0x405b5140>, #env={"SERVER_NAME"=>"192.168.30.146", "HTTP_HOST"=>"192.168.30.146", "HTTP_ACCEPT"=>"text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8", "HTTP_USER_AGENT"=>"Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:88.0) Gecko/20100101 Firefox/88.0", "SERVER_PROTOCOL"=>"HTTP/1.1", "HTTP_ACCEPT_LANGUAGE"=>"es-ES,es;q=0.8,en-US;q=0.5,en;q=0.3", "SERVER_SOFTWARE"=>"lighttpd/1.4.13", "REMOTE_ADDR"=>"192.168.30.29", "PATH_INFO"=>"", "SERVER_ADDR"=>"192.168.30.146", "SCRIPT_NAME"=>"/app/app.app", "HTTP_UPGRADE_INSECURE_REQUESTS"=>"1", "HTTP_COOKIE"=>"lang=en; dispzone=client; sid=ad7ee82e6731416ef7f1d0e80b266906", "REMOTE_PORT"=>"51580", "REQUEST_URI"=>"/app/app.app?cmd=authority_login&username=User&userpass=password", "SERVER_PORT"=>"80", "PATH"=>"/bin:/usr/bin:/sbin:/usr/sbin", "DOCUMENT_ROOT"=>"/espec/html/", "REQUEST_METHOD"=>"GET", "SCRIPT_FILENAME"=>"/espec/html/app/app.app", "GATEWAY_INTERFACE"=>"CGI/1.1", "QUERY_STRING"=>"cmd=authority_login&username=User&userpass=password", "REDIRECT_STATUS"=>"200", "HTTP_ACCEPT_ENCODING"=>"gzip, deflate", "HTTP_CONNECTION"=>"keep-alive"}, #err=#<StringIO:0x405b50b0>>>
I get a warning message from Wireshark saying that Illegal characters found in header name. Is there a way to "skip" this header so my app can read the content of the web?
Thank you in advance!

C# Empty WebClient Downloadstring

I'm trying to download the html string of a website. The website has te following url:
https://www.gastrobern.ch/de/service/aus-weiterbildung/wirtekurs/234/?oid=1937&lang=de
First I tried to do a simple WebClient Request:
var wc = new WebClient();
string websitenstring = "";
websitenstring = wc.DownloadString("http://www.gastrosg.ch/default.asp?id=3020000&siteid=1&langid=de");
But, the websiteString was empty. Then, I read in some posts, that I have to send some additional headerinformations :
var wc = new WebClient();
string websitenstring = "";
wc.Headers[HttpRequestHeader.Accept] = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8";
wc.Headers[HttpRequestHeader.AcceptEncoding] = "gzip, deflate, br";
wc.Headers[HttpRequestHeader.AcceptLanguage] = "de-DE,de;q=0.9,en-US;q=0.8,en;q=0.7";
wc.Headers[HttpRequestHeader.CacheControl] = "max-age=0";
wc.Headers[HttpRequestHeader.Host] = "www.gastrobern.ch";
wc.Headers[HttpRequestHeader.Upgrade] = "www.gastrobern.ch";
wc.Headers[HttpRequestHeader.UserAgent] = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36";
websitenstring = wc.DownloadString("https://www.gastrobern.ch/de/service/aus-weiterbildung/wirtekurs/234/?oid=1937&lang=de");
I tried this, but no answer. Then, I also tried to set some cookies:
wc.Headers.Add(HttpRequestHeader.Cookie,
"CFID=10609582;" +
"CFTOKEN=32721418;" +
"_ga=GA1.2.37" +
"_ga=GA1.2.379124242.1539000256;" +
"_gid=GA1.2.358798732.1539000256;" +
"_dc_gtm_UA-1237799-1=1;");
But this also didn't work. I also found out, that the Browser is somehow doing multiple requests, and my C-Sharp Application is just doing one and showing the first response headers.
But I don't know how I can make a following up request. I'm thankful for every answer.
Try HttpClient instead
Here is an Example On how to use it
public async static Task<string> GetString(string url)
{
HttpClient client = new HttpClient();
// Way around to avoid Deadlock
HttpResponseMessage message = await client.GetAsync(url).ConfigureAwait(false);
return await message.Content.ReadAsStringAsync().ConfigureAwait(false);
}
To call this Method
string dataFromServer = GetString("https://www.gastrobern.ch/de/service/aus-weiterbildung/wirtekurs/234/?oid=1937&lang=de").Result;
I checked Here dataFromServer has HTML content to that page

C# HttpWebResponse not seeing cookies

I'm trying to log in to Yahoo! using an HttpWebRequest, but i'm having trouble getting the initial cookie that they set. I'm not sure if this is a problem with my Request/Response, if they set the cookie in some nefarious way to prevent this kind of activity.
So here's the first part of my Connect() method, which to start with simply gets the login page, so I get the authentication hidden fields and cookies:
public void Connect()
{
var LoginUrl = "https://login.yahoo.com/config/login";
var cookieContainer = new CookieContainer();
// First get a login page to grab some important values
var request = WebRequest.Create(LoginUrl) as HttpWebRequest;
request.Method = "GET";
request.CookieContainer = cookieContainer;
Console.WriteLine(request.SupportsCookieContainer);
var response = request.GetResponse() as HttpWebResponse; /* LINE:30 */
var loginPageText = string.Empty;
using (var reader = new StreamReader(response.GetResponseStream()))
{
loginPageText = reader.ReadToEnd();
}
}
If I inspect the response object at line 30, I don't even see any Set-Cookie headers. If I visit the same page manually in Chrome, I see the following header being sent back:
Set-Cookie:B=bgg40ppbditpf&b=3&s=4s; expires=Mon, 05-Mar-2018 11:53:19 GMT; path=/; domain=.yahoo.com
What could cause those headers to not appear?
I see no cookies either, but if I fake being a browser:
request.UserAgent = "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.75 Safari/537.36";
response.Cookies[0] is set.

Can not set Headers of Content-Type windows phone app

i'm using an HttpClient to create a HTTP request and that client comes from the assembly Windows.Web.Http
All is good when posting the request without the Content-Type Header but the server does not return what I need because it needs that header, so after finding the correct headers needed to be sent I'm facing another problem... I'm not being able to set the Content-Type header
Here is my code (where is the try block is where the error is)
using (var wp = new Windows.Web.Http.HttpClient())
{
HttpRequestMessage mSent = new HttpRequestMessage(HttpMethod.Post, new Uri(url));
//mSent.Headers.Add("Host", "academicos.ubi.pt");
//mSent.Headers.Add("Connection", "keep-alive");
//mSent.Headers.Add("Content-Length", "18532");
//mSent.Headers.Add("Origin", "https://academicos.ubi.pt");
//mSent.Headers.Add("X-Requested-With", "XMLHttpRequest");
//mSent.Headers.Add("Cache-Control", "no-cache");
//mSent.Headers.Add("X-MicrosoftAjax", "Delta=True");
mSent.Headers.Add("User-Agent", " Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2062.120 Safari/537.36");
try
{
mSent.Content.Headers.ContentType = new Windows.Web.Http.Headers.HttpMediaTypeHeaderValue("application/x-www-form-urlencoded");
}
catch (Exception ex) { ex.ToString(); }
//mSent.Headers.Add("Accept", "*/*");
//mSent.Headers.Add("Referer", "https://academicos.ubi.pt/online/horarios.aspx");
//mSent.Headers.Add("Accept-Encoding", "gzip,deflate");
//mSent.Headers.Add("Accept-Language", "pt-PT,pt;q=0.8,en-US;q=0.6,en;q=0.4");
mSent.Headers.Add("Cookie", "the cookie string is big, so I will not post it here");
mSent.Content = new HttpStringContent("the content is well defined, but I will not post it here it's huge"), Windows.Storage.Streams.UnicodeEncoding.Utf8);
HttpResponseMessage mReceived = await wp.SendRequestAsync(mSent, HttpCompletionOption.ResponseContentRead);
if (mReceived.IsSuccessStatusCode)
{
htmlPage = await mReceived.Content.ReadAsStringAsync();
}
}
The error that I'm recieving is Object reference not set to an instance of an object.
I tryied setting this header like I set the user agent it gives me another exception that says that to set the content type I need to set it under content headers...
Any Ideias? I tryied searching for answers for this problem, so far I came out empty handed
Pedro, you need to set the Content property to something, e.g.:
HttpRequestMessage mSent = new HttpRequestMessage(
HttpMethod.Post,
new Uri(url));
mSent.Content = new HttpStringContent(
"Name=Jonathan+Doe&Age=23",
UnicodeEncoding.Utf8,
"application/x-www-form-urlencoded");
There are other kinds of IHttpContent, such as HttpBufferContent, HttpFormUrlEncodedContent, HttpMultipartContent, HttpMultipartFormDataContent and HttpStreamContent.
You do not set the headers on the .Content member. You chould instead use the Multi-Part Content object and set the content similar to this:
HttpMultipartFormDataContent form = new HttpMultipartFormDataContent();
form.Add(new HttpStringContent(RequestBodyField.Text), "data");
HttpResponseMessage response = await httpClient.PostAsync(resourceAddress, form).AsTask(cts.Token);
Ref: http://code.msdn.microsoft.com/windowsapps/HttpClient-sample-55700664/sourcecode?fileId=98924&pathId=1116044733

Can't download utf-8 web content

I have simple code for getting response from a vietnamese website: http://vnexpress.net , but there is a small problem. For the first time, it downloads ok, but after that, the content contains unknown symbols like this:�\b\0\0\0\0\0\0�\a`I�%&/m.... What is the problem?
string address = "http://vnexpress.net";
WebClient webClient = new WebClient();
webClient.Headers.Add("user-agent", "Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.97 Safari/537.11 AlexaToolbar/alxg-3.1");
webClient.Encoding = System.Text.Encoding.UTF8;
return webClient.DownloadString(address);
You'll find that the response is GZipped. There doesn't appear to be a way to download that with WebClient, unless you create a derived class and modify the underlying HttpWebRequest to allow automatic decompression.
Here's how you'd do that:
public class MyWebClient : WebClient
{
protected override WebRequest GetWebRequest(Uri address)
{
var req = base.GetWebRequest(address) as HttpWebRequest;
req.AutomaticDecompression = DecompressionMethods.GZip;
return req;
}
}
And to use it:
string address = "http://vnexpress.net";
MyWebClient webClient = new MyWebClient();
webClient.Headers.Add("user-agent", "Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.97 Safari/537.11 AlexaToolbar/alxg-3.1");
webClient.Encoding = System.Text.Encoding.UTF8;
return webClient.DownloadString(address);
try with code and you'll be fine:
string address = "http://vnexpress.net";
WebClient webClient = new WebClient();
webClient.Headers.Add("user-agent", "Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.97 Safari/537.11 AlexaToolbar/alxg-3.1");
return Encoding.UTF8.GetString(Encoding.Default.GetBytes(webClient.DownloadString(address)));
DownloadString requires that the server correctly indicate the charset in the Content-Type response header. If you watch in Fiddler, you'll see that the server instead sends the charset inside a META Tag in the HTML response body:
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
If you need to handle responses like this, you need to either parse the HTML yourself or use a library like FiddlerCore to do this for you.

Categories