C# Always Getting Empty Cookies - c#

So I always try to see any site cookies but i get empty string
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);
Site Cookies

You haven't sent any request to get cookie. Add Execute method call to your code
var request = new RestRequest(Method.GET);
client.Execute(request); //add this line
var cookie = client.CookieContainer.GetCookieHeader(new Uri("https://server.com"));

Related

HttpClient: "HttpRequestException: An error occurred while sending the request"

I'm trying to get a image using HttpClient and I'm getting this error:
HttpRequestException: An error occurred while sending the request
Using WebClient with DownloadData method, works fine.
var cookieContainer = new CookieContainer();
using (var handler = new HttpClientHandler() { CookieContainer = cookieContainer })
using (var client = new HttpClient(handler))
{
client.DefaultRequestHeaders.Add("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36");
client.DefaultRequestHeaders.Add("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8");
client.DefaultRequestHeaders.Add("Accept-Encoding", "gzip, deflate");
client.DefaultRequestHeaders.Add("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36");
client.DefaultRequestHeaders.Add("Accept-Language", "pt-BR,pt;q=0.8,en-US;q=0.6,en;q=0.4");
client.DefaultRequestHeaders.Add("Upgrade-Insecure-Requests", "1");
client.GetAsync("cookieGenerateUrl").Wait();
client.DefaultRequestHeaders.Remove("Accept");
client.DefaultRequestHeaders.Add("Accept", "image/webp,image/apng,image/*,*/*;q=0.8");
var imagem = client.GetByteArrayAsync(imageUrl).Result;
}
What is the equivalent to WebClient.DownloadData on HttpClient class?
Probably the problem here is that you are calling client.GetByteArrayAsync with no proper handling of the awaitable.
the fix is:
var res = await client.GetByteArrayAsync(url);
or
byte[] arr;
client.GetByteArrayAsync(url).ContinueWith((x) => arr = x.Result);

Facebook Access Token - Automate Getting the Token

Go below to see answer
I am getting the Facebook access token fine, but where I am having troubles is when I am trying to automate this process.
If I visit this URL is the browser I get the access token just fine.
Example:
I paste this into the browser and hit return.
https://www.facebook.com/dialog/oauth?client_id=324234343434&scope=['ads_read', 'ads_management']&redirect_uri=http://www.kb-demos.com/login_success.html&response_type=token
Then I get sent to this page:
http://www.kb-demos.com/login_success.html?#access_token=34543534534534KJ534LKJLKJLKHLH4534534J5KH345KJ3L4H53KJ5H3K4LJH34KH54K&expires_in=5180653
I changed the access token piece so its not a real token
Viola access token!
What I am trying to do is replicate that same behavior with code. I am getting close but not quite there.
I keep getting the user_denied error.
%3Ferror%3Daccess_denied%26error_code%3D200%26error_description%3DPermissions%2Berror%26error_reason%3Duser_denied%23_%3D_&display=page&locale=en_US&logger_id=786a3153-1d81-415c-8dca-f8fa8b0cd630
I am outputting all headers to the console. It is the location header I am concerned with**
I think it has to do with the 302 redirect?
ApplicationId = request.ClientId;
string permissions = "['ads_management', 'ads_read']";
var destinationURL = String.Format(
#"https://www.facebook.com/dialog/oauth?client_id={0}&scope={1}&redirect_uri=http://www.kb-demos.com/login_success.html&response_type=token",
ApplicationId,
permissions);
// Create a new 'HttpWebRequest' Object to the mentioned URL.
HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(destinationURL);
myHttpWebRequest.UserAgent = "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36";
myHttpWebRequest.AllowAutoRedirect = false;
// Assign the response object of 'HttpWebRequest' to a 'HttpWebResponse' variable.
HttpWebResponse myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse();
//Console.WriteLine("\nThe HttpHeaders are \n\n\tName\t\tValue\n{0}", myHttpWebRequest.Headers); // my http headers
// Print the HTML contents of the page to the console.
var headers = myHttpWebResponse.Headers;
// output all the headers
foreach(var header in headers) {
Console.WriteLine(header.ToString() + ": " + headers[header.ToString()] + "\n" );
}
#region
//Stream streamResponse = myHttpWebResponse.GetResponseStream();
//StreamReader streamRead = new StreamReader(streamResponse);
//Char[] readBuff = new Char[256];
//int count = streamRead.Read(readBuff, 0, 256);
//Console.WriteLine("\nThe HTML contents of page the are : \n\n ");
//while (count > 0)
//{
// String outputData = new String(readBuff, 0, count);
// Console.Write(outputData);
// count = streamRead.Read(readBuff, 0, 256);
//}
//// Close the Stream object.
//streamResponse.Close();
//streamRead.Close();
// Release the HttpWebResponse Resource.
#endregion
myHttpWebResponse.Close();
Console.ReadLine();
I am getting a user_denied error here. But in the browser I am getting a perfectly good token. I cannot figure out why.
The headers in the location header it seems to work when using the browser.
Possible scenario if I cannot get the above to work:
I was wondering if there is a browser with an API? Something I can call from the command line - pass in some arguments - and then get the redirect url is a variable to parse?
This code will automate the retrieval of the access_token. You must have credentials to the account you are requesting an access token for.
Updated
First login to the facebook account.
// LOG INTO FACEBOOK ACCT
string email = "youremail#blah.com";
string pw = "yourPassWord";
CookieContainer cookieJar = new CookieContainer();
HttpWebRequest request1 = (HttpWebRequest)WebRequest.Create("https://www.facebook.com");
request1.CookieContainer = cookieJar;
request1.UserAgent = "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36";
//Get the response from the server and save the cookies from the first request..
HttpWebResponse response = (HttpWebResponse)request1.GetResponse();
var cookies = response.Cookies;
cookieJar.Add(cookies);
response.Close();// close the response
string getUrl = "https://www.facebook.com/login.php?login_attempt=1";
string postData = String.Format("email={0}&pass={1}", email, pw);
HttpWebRequest getRequest = (HttpWebRequest)WebRequest.Create(getUrl);
getRequest.CookieContainer = cookieJar;
//Adding Previously Received Cookies
getRequest.CookieContainer.Add(cookies);
getRequest.Method = WebRequestMethods.Http.Post;
getRequest.UserAgent = "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36";
getRequest.AllowWriteStreamBuffering = true;
getRequest.ProtocolVersion = HttpVersion.Version11;
getRequest.AllowAutoRedirect = true;
getRequest.ContentType = "application/x-www-form-urlencoded";
byte[] byteArray = Encoding.ASCII.GetBytes(postData);
getRequest.ContentLength = byteArray.Length;
Stream newStream = getRequest.GetRequestStream(); //open connection
newStream.Write(byteArray, 0, byteArray.Length); // Send the data.
newStream.Close();
HttpWebResponse getResponse = (HttpWebResponse)getRequest.GetResponse();
using (StreamReader sr = new StreamReader(getResponse.GetResponseStream()))
{
string sourceCode = sr.ReadToEnd();
}
Then request the access_token
ApplicationId = request.ClientId; // your application id
string permissions = "['ads_management', 'ads_read']";
var destinationURL = String.Format(
#"https://www.facebook.com/dialog/oauth?client_id={0}&scope={1}&redirect_uri=http://www.kb-demos.com/login_success.html&response_type=token",
ApplicationId,
permissions);
// Create a new 'HttpWebRequest' Object to the mentioned URL.
HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(destinationURL);
// use the same cookie container and cookies
myHttpWebRequest.CookieContainer = cookieJar;
myHttpWebRequest.CookieContainer.Add(cookies); //recover cookies First request
myHttpWebRequest.UserAgent = "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36";
myHttpWebRequest.AllowAutoRedirect = false;
// Assign the response object of 'HttpWebRequest' to a 'HttpWebResponse' variable.
HttpWebResponse myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse();
Console.WriteLine("\nThe Request HttpHeaders are \n\n\tName\t\tValue\n{0}", myHttpWebRequest.Headers); // my http headers
Console.WriteLine("\nThe Request HttpHeaders are \n\n\tName\t\tValue\n{0}", myHttpWebRequest.CookieContainer); // my http headers
//Console.WriteLine("\nThe Request HttpHeaders are \n\n\tName\t\tValue\n{0}", cookies); // my http headers
var headers = myHttpWebResponse.Headers;
// output all the headers
foreach (var header in headers)
{
Console.WriteLine(header.ToString() + ": " + headers[header.ToString()] + "\n");
}
var cow = GetParams(headers["Location"]);
string accessToken = "";
accessToken = cow["#access_token"];
And the helper method
/// <summary>
/// Helper method to get Params from URL using RegEx
/// </summary>
static Dictionary<string, string> GetParams(string uri)
{
var matches = Regex.Matches(uri, #"[\?&](([^&=]+)=([^&=#]*))", RegexOptions.Compiled);
return matches.Cast<Match>().ToDictionary(
m => Uri.UnescapeDataString(m.Groups[2].Value),
m => Uri.UnescapeDataString(m.Groups[3].Value)
);
}

Error 500 with authorization while consuming OAuth2 RESTful service through C#

My current job is to consume a RESTful API with OAuth2. Currently I worked out how to get the access token and it is working ok while I use the chrome extension Rest Console, but when I try to do it from my application I always get the error that I am sending an invalid OAuth request. Below you can see three of the ways I tried to consume the API, but to no success. The page always returns error 500. Any help will be appreciated, if I had missed something crucial.
var auth = "Bearer " + item.access_token;
/* First Attempt */
var client = new RestClient("http://<link>");
var request = new RestRequest("sample", Method.GET);
request.AddHeader("Authorization", auth);
request.AddHeader("Content-Type", "application/json;charset=UTF-8");
request.AddHeader("Pragma", "no-cache");
request.AddHeader("User-Agent", "User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.118 Safari/537.36");
request.AddHeader("Accept", "application/json");
request.RequestFormat = DataFormat.Json;
var response = client.Execute(request);
var content = response.Content;
/* Second Attempt */
string sURL = "http://<link>/sample";
string result = "";
using (WebClient client = new WebClient())
{
client.Headers["Authorization"] = auth;
client.Headers["Content-Type"] = "application/json;charset=UTF-8";
client.Headers["Pragma"] = "no-cache";
client.Headers["User-Agent"] = "User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.118 Safari/537.36";
client.Headers["Accept"] = "application/json";
byte[] byteArray = Encoding.UTF8.GetBytes(parameters);
var result1 = client.DownloadString(sURL);
}
/* Third Attempt */
var request = (HttpWebRequest)WebRequest.Create(sURL);
request.Method = "GET";
request.ContentType = "application/json;charset=UTF-8";
request.Accept = "application/json";
request.Headers["Authorization"] = auth;
request.UserAgent = "User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.118 Safari/537.36";
string content;
HttpStatusCode statusCode;
using (var response = request.GetResponse())
using (var stream = response.GetResponseStream())
{
var contentType = response.ContentType;
Encoding encoding = null;
if (contentType != null)
{
var match = Regex.Match(contentType, #"(?<=charset\=).*");
if (match.Success)
encoding = Encoding.GetEncoding(match.ToString());
}
encoding = encoding ?? Encoding.UTF8;
statusCode = ((HttpWebResponse)response).StatusCode;
using (var reader = new StreamReader(stream, encoding))
content = reader.ReadToEnd();
}
--------EDIT--------
For the first attempt I also tried to add the authentication to the client variable client.Authenticator = Authenticate; where OAuth2AuthorizationRequestHeaderAuthenticator Authenticate = new OAuth2AuthorizationRequestHeaderAuthenticator(item.access_token, item.token_type);
The code seems right. The fail attempts you did suggest that the issue is with the token and not the code. Bearer tokens have expiration time. So semms like your token expired between the first time you got it using chrome REST Console extension and when you wrote your code. But the strange situation here is the 500 error code you got. 401 is standard response when you token expired or not exist. 500 error code always mean server error.

WebClient login and password

I need to enter username/password to the web https://secure.domaintools.com/log-in/?logout
HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
NameValueCollection nameValues = new NameValueCollection();
ServicePointManager.Expect100Continue = false;
nameValues.Add("username", "ipdnstest#gmail.com");
nameValues.Add("password", "ipdnstest");
using (var wc = new WebClient())
{
wc.UploadValues("https://secure.domaintools.com/log-in/?logout", nameValues);
wc.Headers["User-Agent"] = "MOZILLA/5.0 (WINDOWS NT 6.1; WOW64) APPLEWEBKIT/537.1 (KHTML, LIKE GECKO) CHROME/21.0.1180.75 SAFARI/537.1";
string page = wc.DownloadString("https://secure.domaintools.com/my-account/");
doc.LoadHtml(page);
doc.Save("f.htm");
}
But in file "f.htm" still https://secure.domaintools.com/log-in/?logout web, so i didn't log in successfully
Can you help me with that? login/password are real.

WebClient login and password to the Website

I'm trying to log in to https://secure.domaintools.com/log-in/?logout (http://whois.domaintools.com)
using (var myWebClient = new WebClient())
{
string loginData = "username=test#test.com&passowrd=pwd";
string response = myWebClient.UploadString("https://secure.domaintools.com/log-in/?logout", "POST", loginData);
myWebClient.Headers["User-Agent"] = "MOZILLA/5.0 (WINDOWS NT 6.1; WOW64) APPLEWEBKIT/537.1 (KHTML, LIKE GECKO) CHROME/21.0.1180.75 SAFARI/537.1";
myWebClient.DownloadString("http://whois.domaintools.com");
doc.LoadHtml(page);
}
But have this error
Error 417 Expectation failed
How can I fix that?
Add this property to your code
ServicePointManager.Expect100Continue = false;

Categories

Resources