I have the following Image, when this image opened using any browser and on either normal or private mode it works perfectly.
but using HttpRequest & HttpResponse it produces the following:
Here is my code:
/// <summary>
///
/// </summary>
/// <param name="imageUrl"></param>
/// <returns></returns>
private static Image GetImage(string imageUrl)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(imageUrl);
request.Method = "GET";
request.KeepAlive = true;
request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8";
request.Host = "www.google.com";
request.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1700.107 Safari/537.36";
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
return Image.FromStream(response.GetResponseStream());
}
And here is the Raw HttpRequest from Fiddler:
GET https://www.google.com/recaptcha/api/image?c=03AHJ_VusG9FppmdZqKUPjxoVUV4290vtoCQDM8jw9BROEI396sx7J4YYxUBab1zEQg2T94n19CiW2ahGLAtGm5H7nEgckTtJACFcXGZg_frhYIsBAg8VaY5JAbP4oJCx3cmknqltFsYeDUrdv0ZWbFjcMtYuZ0FQnh3blc5ZEx1oJuWJpLzPy0WhhZUIgJ_EXpiC5w-pvt00P HTTP/1.1
Host: www.google.com
Connection: keep-alive
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1700.107 Safari/537.36
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-US,en;q=0.8,ar;q=0.6
Can anybody help in getting this image successfully. Thanks.
Related
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);
Below is what the request looks like in the web app.
Request URL:http://myurl.com/rest
Request Method:PUT
Status Code:200 Ok
Request Headersview source
Accept:*/*
Accept-Encoding:gzip, deflate, sdch
Accept-Language:en-US,en;q=0.8
Connection:keep-alive
Content-Length:60
Content-Type:application/json
Cookie:ui_dom_other=block; session=sessionkey; acct_table#pageNavPos=1; ui_usr_feat=block
Host:http://myurl
Origin:http://myurl.com
Referer:http://myurl.com/referer
User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36
Request Payload view parsed
{"table":"users","settings":[{"name":"dnd","value":"true"}]}
Response Headersview source
Cache-Control:no-cache
Content-Length:2
Content-Type:application/json
The data is in the request payload field
{"table":"users","settings":[{"name":"dnd","value":"true"}]}
Below is my current C# RestSharp code
// Initiate Rest Client
var client = new RestClient("http://myurl.com");
var request = new RestRequest("resturl/restrequest");
// Set headers, method and cookies
request.Method = Method.PUT;
request.AddHeader("Content-Type", "application/json");
request.AddHeader("Accept", "*/*");
request.AddCookie("session", sessionKey);
// Set Data format
request.RequestFormat = DataFormat.Json;
// Set Data
string theString = "{'table':'users','settings':[{'name':'dnd','value':'true'}]}";
request.AddBody(theString);
// Execute
var test123 = client.Execute(request);
I've been able to successfully do all my GET/POST calls however PUT has not been successful.
Fiddler Capture
Web App - Working
PUT http://myurl HTTP/1.1
Host: http://myurl
Connection: keep-alive
Content-Length: 60
Origin: hhttp://myurl
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36
Content-Type: application/json
Accept: */*
Referer: http://myurl
Accept-Encoding: gzip, deflate, sdch
Accept-Language: en-US,en;q=0.8
Cookie: ui_dom_other=block; session=sessionkey; acct_table#pageNavPos=1; ui_usr_feat=block
{"table":"users","settings":[{"name":"dnd","value":"true"}]}
C# Application - Not Working
PUT http://myurl HTTP/1.1
Accept: */*
User-Agent: RestSharp/105.0.1.0
Content-Type: text/xml
Host: http://myurl
Cookie: session=sessionkey
Content-Length: 10
Accept-Encoding: gzip, deflate
<String />
Instead of adding a string as the payload consider adding an object:
request.AddBody(new { table = "users", settings = new[] { new { name = "dnd", value = "true" } } });
I want to sign in to a site when a link is clicked and then redirect the browser there with a signed in session. Im having some troubles and here is what Ive tried:
First I get the session cookies from the login site:
CookieContainer cookies= new CookieContainer();
HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create("http://someuri.com");
myHttpWebRequest.CookieContainer = cookies;
HttpWebResponse myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse();
myHttpWebResponse.Close();
Then I post to the sign in page to get signed in:
HttpWebRequest getRequest = (HttpWebRequest)WebRequest.Create("http://signInURL.com");
getRequest.CookieContainer = cookies;
getRequest.Method = WebRequestMethods.Http.Post;
getRequest.UserAgent = "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/535.2 (KHTML, like Gecko) Chrome/15.0.874.121 Safari/535.2";
getRequest.AllowWriteStreamBuffering = true;
getRequest.ProtocolVersion = HttpVersion.Version11;
getRequest.AllowAutoRedirect = true;
getRequest.ContentType = "application/x-www-form-urlencoded";
byte[] byteArray = Encoding.ASCII.GetBytes(PostParameterStringWithSignInInfo);
getRequest.ContentLength = byteArray.Length;
Stream newStream = getRequest.GetRequestStream();
newStream.Write(byteArray, 0, byteArray.Length);
newStream.Close();
HttpWebResponse getResponse = (HttpWebResponse)getRequest.GetResponse();
Then I figured I need to set the cookies to the client:
CookieCollection cooki = getRequest.CookieContainer.GetCookies(new Uri("http://someUri.com"));
for(int i = 0; i < cooki.Count; i++)
{
Cookie c = cooki[i];
Response.Cookies.Add(new HttpCookie(c.Name, c.Value));
}
And then redirect to where you end up being signed in:
Response.Redirect("http://URLwhenBeingSignedIn.com");
This doesnt work. When redirected Im still logged out.
Tried to do this with Fiddler and succeeded to sign in and get redirected:
Get the session cookies:
GET / HTTP/1.1
Content-type: application/x-www-form-urlencoded
Host: someuri.com
Post to the sign in page to get signed in:
POST /signIn HTTP/1.1
Accept: text/html, application/xhtml+xml, */*
Referer: http://someuri.com
Accept-Language: en-GB,en;q=0.7,tr;q=0.3
User-Agent: Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; WOW64; Trident/6.0)
Content-Type: application/x-www-form-urlencoded
Accept-Encoding: gzip, deflate
Connection: Keep-Alive
Content-Length: 90
DNT: 1
Host: signInURL.com
Pragma: no-cache
Cookie: JSESSIONID=fromBefore; Cookie2=fromBefore
PostParameterStringWithSignInInfo
Perhaps there's an easier way than the one I thought of now that you can see the fiddler requests that works, if so I'm happy to see it.
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";
...
}