I have a third party URL which sends the oAuth parameters in the header. When i am reading it using the below code the Authorization parameters are not being returned. What am i missing?
string url = "http: // xxx.com/events/abc-def";
HttpClient httpClient = new HttpClient();
WebRequest request = WebRequest.Create(url);
request.Method = WebRequestMethods.Http.Get;
string authHeader = request.Headers["Authorization"];
Related
I am trying to get request auth token by making a post web request to a url. The api expects username/password as credentials in the form-data payload.
When I click the sign-in option on the browser, the network logs show a GET request with HTML as response, followed by a POST request which returns form-data with username/password and request token in payload.
Trying to mock the flow using webrequest, I am doing a simple post request, as the following:
public string HttpPost(string url, string post, string refer = "")
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
// request.CookieContainer = cJar;
request.UserAgent = UserAgent;
request.KeepAlive = false;
request.Method = "POST";
request.Referer = refer;
byte[] postBytes = Encoding.ASCII.GetBytes(post);
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = postBytes.Length;
Stream requestStream = request.GetRequestStream();
requestStream.Write(postBytes, 0, postBytes.Length);
requestStream.Close();
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
StreamReader sr = new StreamReader(response.GetResponseStream());
return sr.ReadToEnd();
}
However, this request only returns the text/HTML markup of the page as the first part of the request of the browser does. How do I get it to run the subsequent POST to fetch the token from the endpoint?
EDIT 1:
Here is the first GET Request:
The token is a CSRF token, what you need to do is find the login form in the html response that you've received with your initial get request, and also to ensure you are storing the cookies set in this response.
You will then need to search within the html response for the hidden input parameter named 'token' next to the username and pw input fields and use the value of that element to compose your post request.
Doing this programmatically is possible with some regex or the htmlagilitypack to extract that token
Is there a way to get the full url of a RestSharp request including its resource and querystring parameters?
I.E for this request:
RestClient client = new RestClient("http://www.some_domain.com");
RestRequest request = new RestRequest("some/resource", Method.GET);
request.AddParameter("some_param_name", "some_param_value", ParameterType.QueryString);
IRestResponse<ResponseData> response = client.Execute<ResponseData>(request);
I would like to get the full request URL:
http://www.some_domain.com/some/resource?some_param_name=some_param_value
To get the full URL use RestClient.BuildUri()
Specifically, in this example use client.BuildUri(request):
RestClient client = new RestClient("http://www.some_domain.com");
RestRequest request = new RestRequest("some/resource", Method.GET);
request.AddParameter("some_param_name", "some_param_value", ParameterType.QueryString);
IRestResponse<ResponseData> response = client.Execute<ResponseData>(request);
var fullUrl = client.BuildUri(request);
Well, it is pretty tricky.
To get the full requested URL use RestClient.Execute(request).ResponseUri to be sure it is the already sent request.
In this example:
RestClient client = new RestClient("http://www.some_domain.com");
RestRequest request = new RestRequest("some/resource", Method.GET);
request.AddParameter("some_param_name", "some_param_value", ParameterType.QueryString);
IRestResponse<ResponseData> response = client.Execute<ResponseData>(request);
Uri fullUrl = response.ResponseUri;
This code:
Console.WriteLine(string.Format("response URI: {0}", response.ResponseUri.ToString()));
returns:
response URI: http://www.some_domain.com/some/resource?some_param_name=some_param_value
How can I send an HTTP request with Negotiate Authorization header attribute from a .NET (C#) application?
I tried the following, but Authorization attribute was not added to the request...
...
string url = ...;
WebRequest request = WebRequest.Create(url);
request.Credentials = GetCredential();
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
...
private CredentialCache GetCredential()
{
string url = ...;
CredentialCache credentialCache = new CredentialCache();
credentialCache.Add(new System.Uri(url), "Ntlm", new NetworkCredential(username, pwd, domain));
return credentialCache;
}
My experience with using a network credential in a WebRequest is that the request.GetResponse() does NOT pass the credential unless it receives an Unauthorized (challenge) response from the server. If it does receive a 403, it will automatically fire a second request which includes the credential. Make sure the end point you are hitting returns a 401 if the Auth header is missing.
I'm trying to send a cookie with a request to an Asp.net page. From what I understand it expects the cookies value to NOT be encoded. If I encode the value it doesn't register it.
CookieContainer won't let me add the non encoded value to it though. I can't seem to find a work around...
My code is essentially
HttpWebRequest request = (HttpWebRequest) HttpWebRequest.Create(#"http:\\whatever");
string cookieName = "whatevername";
string cookieValue = "version=1&Type=a,b,c,d,e,f";
Cookie cook = new Cookie(cookieName, cookieValue, "/", "mypage");
CookieContainer cookies = new CookieContainer();
cookies.Add(cook);
request.CookieContainer = cookies;
This throws a CookieException saying "The 'Value'='version=1&Type=a,b,c,d,e,f' part of the cookie is invalid."
You could try UrlEncoding value.
HttpWebRequest request = (HttpWebRequest) HttpWebRequest.Create(#"http:\\whatever");
string cookieName = "whatevername";
string cookieValue = "version=1&Type=a,b,c,d,e,f";
Cookie cook = new Cookie(cookieName, Server.UrlEncode(cookieValue), "/", "mypage");
CookieContainer cookies = new CookieContainer();
cookies.Add(cook);
request.CookieContainer = cookies;
Trying to figure out if I need to escape characters in my header value. Much like the example belowif I'm sending a header authorization over for OAuth to a server resource:
“PUT /api/v1/articles/6.json HTTP/1.1\r\nAccept: /\r\nUser-Agent: OAuth gem v0.4.5\r\nContent-Length: 9\r\nContent-Type: application/x-www-form-urlencoded\r\nAuthorization: OAuth oauth_consumer_key=\”nMu4u9pLRfDrxhPVK5yn\“, oauth_nonce=\”5346IG1e5bV3ytQwdFqkP8Rgr0VJiA9Xb4FE0\“, oauth_signature=\”64545G%2Byp%2F2BsqJ%2BUUgbjIIIV9E%3D\“, oauth_signature_method=\”HMAC-SHA1\“, oauth_timestamp=\”1330022891\“, oauth_token=\”ivouGxpsJbyIU5viPKOO\“, oauth_version=\”1.0\“\r\nConnection: close\r\nHost: someHostNameHere\r\n\r\n”
Right now I'm sending it (the value portion for my Authorization collection) over as one big string like this for my value for the authorization key:
"OAuth oauth_consumer_key=Mu4u9pLRfDrxhPVK5y, oauth_nonce=5346IG1e5bV3ytQwdFqkP8Rgr0VJiA9Xb4FE0, oauth_signature=64545G%2Byp%2F2BsqJ%2BUUgbjIIIV9E%3D, oauth_signature_method=HMAC-SHA1, oauth_timestamp=1330022891, oauth_token=ivouGxpsJbyIU5viPKOO, oauth_version=1.0"
Here's how I would do it:
var url = "https://somedomain.com/resource/v1/";
var request = WebRequest.Create(url) as HttpWebRequest;
request.Method = "POST";
request.ContentType = "text/xml";
var oAuthHeader = "OAuth oauth_consumer_key=Mu4u9pLRfDrxhPVK5y, oauth_nonce=5346IG1e5bV3ytQwdFqkP8Rgr0VJiA9Xb4FE0, oauth_signature=64545G%2Byp%2F2BsqJ%2BUUgbjIIIV9E%3D, oauth_signature_method=HMAC-SHA1, oauth_timestamp=1330022891, oauth_token=ivouGxpsJbyIU5viPKOO, oauth_version=1.0";
request.Headers.Add("Authorization", oAuthHeader);
var response = request.GetResponse();
Not sure why you would want to construct your own raw HttpRequest. Obviously you would call some function to generate your OAuth header values, this is demonstrative only.