I'm writing simple application which connects to webpage and receives specific data from document's body.
I'm sending HttpWebRequest in this way:
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://webpage.com/page1.php");
request.Method = "POST";
string postData = "some_post_data";
byte[] byteArray = Encoding.UTF8.GetBytes(postData);
request.ContentType = "application/x-www-form-urlencoded";
request.Referer = "http://webpage.com/";
request.ContentLength = byteArray.Length;
request.CookieContainer = new CookieContainer();
request.UserAgent = "Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.64 Safari/537.31";
request.Headers.Add("Cookie", string.Format("sess_id={0}", UserInfo.SessionId));
request.Headers.Add("Accept-Charset", "ISO-8859-2,utf-8;q=0.7,*;q=0.3");
request.Headers.Add("Accept-Language", "pl-PL,pl;q=0.8,en-US;q=0.6,en;q=0.4");
request.Headers.Add("Accept-Encoding", "gzip");
request.AutomaticDecompression = DecompressionMethods.GZip;
Stream dataStream = request.GetRequestStream();
dataStream.Write(byteArray, 0, byteArray.Length);
dataStream.Close(); // *** [1] ***
HttpWebResponse response = (HttpWebResponse)request.GetResponse(); // *** [2] ***
dataStream = response.GetResponseStream();
response.Close();
StreamReader reader = new StreamReader(dataStream);
string responseFromServer = reader.ReadToEnd();
Fine! The request is ok, there is nothing magic. The point is, after dataStream.Close(); (line [1]) I receive 303 status code, which means there will be redirect to another page. When I read response at point [2] I see new (redirected) page.
All I want is to read headers before redirect. Is it possible anyway?
Set the AllowAutoRedirect property of request to false prior to calling GetResponse.
request.AllowAutoRedirect = false;
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
More information.
Related
I'm trying login to nike.com with my C# windows application.
var request = (HttpWebRequest)WebRequest.Create("https://unite.nike.com/login?appVersion=376&experienceVersion=318&uxid=com.nike.commerce.nikedotcom.web&locale=en_US&backendEnvironment=identity&browser=Google%20Inc.&os=undefined&mobile=true&native=false&visit=1&visitor=048a11da-9ba2-4a4b-91c3-e3cc2774694c");
string stringData = "appVersion=376&experienceVersion=318&uxid=com.nike.commerce.nikedotcom.web&locale=en_US&backendEnvironment=identity&browser=Google%20Inc.&os=undefined&mobile=true&native=false&visit=1&visitor=048a11da-9ba2-4a4b-91c3-e3cc2774694c";
var data = Encoding.ASCII.GetBytes(stringData);
request.Method = "POST";
request.ContentType = "text/plain";
request.ContentLength = data.Length;
request.UserAgent = "Mozilla/5.0 (Linux; Android 5.0; SM-G900P Build/LRX21T) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.186 Mobile Safari/537.36";
request.Referer = "https://m.nike.com/us/en_us/?l=shop,login_register";
request.Accept = "*/*";
request.Headers.Add("origin", "https://m.nike.com");
request.Headers.Add("cookie", "AnalysisUserId=104.96.91.109.102041520967791940; exp.swoosh.user=%7B%22granted%22%3A0%7D; nike_locale=us/en_us; NIKE_COMMERCE_COUNTRY=US; NIKE_COMMERCE_LANG_LOCALE=en_US; guidU=7d702b13-ba36-4c8a-d743-f726b99d8807; neo.swimlane=52; dreams_sample=44; cicIntercept=1; cicGuestIntercept=1; cto_lwid=a72ef9b0-3206-45ff-91c7-cf41bf80c118; RES_TRACKINGID=303445732649467; ResonanceSegment=1; AMCVS_F0935E09512D2C270A490D4D%40AdobeOrg=1; AKNIKE=CT-2; lls=3; NIKE_COMMERCE_CCR=1520971547200; CONSUMERCHOICE_SESSION=t; CONSUMERCHOICE=us/en_us; bm_sz=BB6980129B3DFBC73F801C679C8953FB~QAAQbVtgaHQolR5iAQAAceRoI+xF8PxoczE2kGatzaDU90mSX4sJNBTWHwhFh7uDbTBpEQHu9AfqzSCrZKBDtteV5n2LhACXIuzQPezvcsAV4beXFH8tNbnpCs2PEvR7OU9em9lBpGFcKorAL8+S5xz0pTfCxfFlBuI/jXzeXOToMSbg4sbv9Z8Cktgy; guidS=eb252880-ba16-468d-f497-070593e9031c; RES_SESSIONID=718716042916119; AMCV_F0935E09512D2C270A490D4D%40AdobeOrg=2121618341%7CMCIDTS%7C17604%7CMCMID%7C50402020119854507093682697974758826217%7CMCAID%7CNONE%7CMCOPTOUT-1521019750s%7CNONE; ak_bmsc=A51447336BE456E9AF9F08FFF8188EAA68605B6DDC27000013CFA85AEF1E1F19~plBWC7o30uP95yZXaGw7lBKJ4dm6/EyPeO9FAJdZdKsRmLIsW/rBraVYu2IUQMMTzazdq8S4a9PTCJGxaaNGIXxedF/Z1NA1A/EdQFeCCKL9nnFtlavdjlwi2eFiKcBQhJ+bcS8k0tuQKJ1cNS2ng6Me92rK7RrBqtg85tNGZytGfTHxJUd7jWWF43tskP9lnsbmau+myiG1dhgpLSG+40zS02iXa3U0uSZv6gLlNxssDgQEJC/orrdIs/XKOxdsHXuqVzq3RsYGyWzaz05xqUrQ==; neo.experiments=%7B%22main%22%3A%7B%223039-interceptor%22%3A%22a%22%2C%223250-interceptor%22%3A%22a%22%7D%2C%22plus%22%3A%7B%7D%2C%22mobile%22%3A%7B%223199-interceptor%22%3A%22a%22%2C%223246-interceptor%22%3A%22a%22%7D%7D; USID=8049283F7E6AF657B21C7DEB135F66F6.sin-239-app-us-0; _abck=71900787CAC90E301D55611832D59B6268605B6DDC2700000720A85A5D67BF01~0~gnbkwgPHLhkJw9twiqxqN8vDHgFrBO4hdMKVy2uNJLY=~-1~-1; s_sess=%20c51%3Dvertical%3B%20s_cc%3Dtrue%3B%20tp%3D5325%3B%20s_ppv%3Dnikecom%25253Ehomepage%252C12%252C12%252C640%3B%20prevList2%3D%3B; slCheck=N; llCheck=N; sls=0; utag_main=_st:1521016412752$ses_id:1521012619059%3Bexp-session; s_pers=%20c58%3Dno%2520value%7C1521016402289%3B%20s_dfa%3Dnikecomprod%7C1521016414639%3B; guidSTimestamp=1521012520794|1521014615096; bm_sv=86FAB72505FE57713A77C0CA3407044F~o/nQM0bVBprEbVN+2VGk4ea4sVZcujD42BoLvCRxpu5NKwgMTO3oYvgHyrOfOp6h8hv1tI0+3tH/0i3tM3eX8PCQLcibu6uf3iTtPDKnHFTtfDkN+8R4UN0nD0p7L9RGwrkRLuzauSMSonmJHxzCD1HCzVft9Pjx0r02SuViioM=; ppd=profile%7Cnikecom%3Eprofile%3Elogin%20start; _uetsid=_uetef349e8d; bc_nike_triggermail=%7B%22distinct_id%22%3A%20%2216220f6b312234-0cca71d41deec1-b353461-1fa400-16220f6b3134a6%22%7D");
request.Headers.Add("accept-language", "en,hr;q=0.9");
request.Headers.Add("accept-encoding", "gzip, deflate, br");
request.Headers.Add("authority", "unite.nike.com");
request.Headers.Add("path", "/login?appVersion=376&experienceVersion=318&uxid=com.nike.commerce.nikedotcom.web&locale=en_US&backendEnvironment=identity&browser=Google%20Inc.&os=undefined&mobile=true&native=false&visit=1&visitor=048a11da-9ba2-4a4b-91c3-e3cc2774694c");
request.Headers.Add("scheme", "https");
var newStream = request.GetRequestStream();
newStream.Write(data, 0, data.Length);
newStream.Close();
WebResponse response = request.GetResponse();
//MessageBox.Show((((HttpWebResponse)response).StatusCode.ToString()));
StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
string responseFromServer = reader.ReadToEnd();
MessageBox.Show(responseFromServer);
reader.Close();
response.Close();
I don't know how to include Request Payload data in my request
With current code I receive response from server:
The remote server returned an error: (401) Unauthorized.
How to include JSON data in my POST request?
I haven't posted here in a long time so please forgive me if I am not formatting this question properly. I am trying to login to a website(omitted) via the .NET objects HttpWebRequest and HttpWebResponse. Using Wireshark, I can see that my POST request is identical to Chrome's POST request when I login to this website through my application. I am having issues getting the full response back though.
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
This response object has all the appropriate response headers, but there is still data I would like to see that is being sent through HTTP chunked responses. I can verify this in Wireshark as well. My understanding is that I need to instantiate a StreamReader object to read this remaining data. My code is blowing up at this line:
using (StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
The stack trace is showing this error:
System.ArgumentException: Stream was not readable.
How can I use this StreamReader object to read the entire response after my POST request is sent? Below is my code that sends the POST request for logging into the website. Please let me know if you have any questions and I will be happy to clarify any confusion.
public bool Login()
{
try
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(#"http://WEBSITE-REMOVED/CheckAccess");
request.CookieContainer = CookieContainer;
//Set POST data.
string postData = "institution=AAA";
postData += "&ismobile=false";
postData += "&id=BBB";
postData += "&password=CCC";
byte[] data = Encoding.ASCII.GetBytes(postData);
//Configure HTTP POST request.
request.Headers.Clear();
request.Method = "POST";
request.Accept = #"text/html, application/xhtml+xml, image/jxr, */*";
request.Referer = #"http://WEBSITE-REMOVED/entry.html";
request.Headers.Add("Accept-Language", "en-US");
request.UserAgent = #"Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; rv:11.0) like Gecko";
request.ContentType = #"application/x-www-form-urlencoded";
request.Headers.Add("Accept-Encoding", "gzip, deflate");
request.Host = "op.responsive.net";
request.ContentLength = data.Length;
request.KeepAlive = true;
request.Headers.Add("Cache-Control", "no-cache");
using (Stream stream = request.GetRequestStream())
{
stream.Write(data, 0, data.Length);
}//using
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
using (StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
{
//TO-DO
}
}//try
catch(Exception ex)
{
File.AppendAllText(ErrorLogDirectory + "Errors.txt", "Login() Exception\r\n" + System.DateTime.Now + "\r\n" + ex.ToString() + Environment.NewLine);
}//catch
return false;
}//Login
I'm having a problem when sending HttpWebRequest to a particular url.
The response is generated but it is not that I want.
It is giving a response like "Session Timeout! Please Login to continue".
Whereas,
When I'm sending the same request through Python 3.4 it is giving me the required response.
The response is like "--some html is here--" which I want.
I don't know why this is happening because I wan't it to do throught C#.
The CodeBehind (giving the wrong response) is:
string postData = "somequerystring";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("someurl");
request.KeepAlive = true;
request.ProtocolVersion = HttpVersion.Version10;
request.UserAgent = "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36";
request.Method = "POST";
byte[] byteArray = Encoding.UTF8.GetBytes(postData);
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = byteArray.Length;
request.Headers.Add(HttpRequestHeader.CacheControl, "no-cache");
using (var dataStream = request.GetRequestStream())
{
dataStream.Write(byteArray, 0, byteArray.Length);
}
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
string responseFromServer = new StreamReader(response.GetResponseStream()).ReadToEnd();
response.Close();
Context.Response.Write(responseFromServer);
The Python Script (giving the correct response) is:
import requests
url = "someurl"
payload = "somequerystring"
headers = {
'content-type': "application/x-www-form-urlencoded"
}
response = requests.request("POST", url, data=payload, headers=headers)
print(response.text)
Help me out?
I am working on simple web application where I need to get access token from outlook API in my app to use employee name and its image.I have written code and and able to login through outlook but my access token is coming as null.Please find my code below:
public void GetResponseCallback(IAsyncResult asynchronousResult)
{
HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState;
// End the operation
HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(asynchronousResult);
Stream streamResponse = response.GetResponseStream();
StreamReader streamRead = new StreamReader(streamResponse);
string responseString = streamRead.ReadToEnd();
access_token = responseString;
// Close the stream object
streamResponse.Close();
streamRead.Close();
// Release the HttpWebResponse
response.Close();
allDone.Set();
}
HttpWebRequest request =(HttpWebRequest)WebRequest.Create("some url");
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.UserAgent = "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 7.1; Trident/5.0)";
request.Accept = "/";
request.UseDefaultCredentials = true;
request.Proxy.Credentials = System.Net.CredentialCache.DefaultCredentials;
doc.Save(request.GetRequestStream());
HttpWebResponse resp = request.GetResponse() as HttpWebResponse;
Hope it helps
i using HttpWebRequest/HttpWebResponse to get html document, the code follow was running but i can not encode received stream to html string:
string uri = "https://myfavoritesite.come";
HttpWebRequest webrequest = (HttpWebRequest)WebRequest.Create(uri);
webrequest.KeepAlive = true;
webrequest.Method = "GET";
webrequest.ContentType = "text/html";
webrequest.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
//webrequest.Connection = "keep-alive";
webrequest.Host = "cat.sabresonicweb.com";
webrequest.Headers.Add("Accept-Encoding", "gzip, deflate");
webrequest.Headers.Add("Accept-Language", "en-US,en;q=0.5");
webrequest.UserAgent = "Mozilla/5.0 (Windows NT 6.1; rv:18.0) Gecko/20100101 Firefox/18.0";
HttpWebResponse webresponse = (HttpWebResponse)webrequest.GetResponse();
Console.Write(webresponse.StatusCode);
Stream receiveStream = webresponse.GetResponseStream();
Encoding enc = System.Text.Encoding.GetEncoding(1252);//1252
StreamReader loResponseStream = new
StreamReader(receiveStream, enc);
string Response = loResponseStream.ReadToEnd();
loResponseStream.Close();
webresponse.Close();
Console.Write(Response);
So, i use below code line to test is there successful request.
Console.Write(webresponse.StatusCode);
The result on the screen was OK, it's mean the request was sent but the Response string expose on screen was not html format, it's something so strange like this: #32u%&$&(#*#Eeeuw
By using webrequest.Headers.Add("Accept-Encoding", "gzip, deflate"); you are telling the server that you understand compressed responses. Remove that header and use a normal UTF8 encoding instead of 1252 that you are using. You should then get the proper string. You can just use System.Text.Encoding.UTF8.