I'm trying to get a captcha from a 3rd party website and store it into a picturebox.
Here is the captcha url: http://www.lbj00.com/ValidationControls.aspx?
Using Google chrome I could view the captcha.
Sample image:
Sample image from chrome
But getting it to work in C#.net seems there is a problem. So far I have tried to use another captcha url: http://www.xl18.biz/general/captcha.aspx from a different 3rd party website and it could store it in a picturebox without a problem.
Here is a sample code what I have from a class:
public static Stream GetStream(string url,out string refcookies)
{
HttpWebResponse httpWebResponse = null;
HttpWebRequest httpWebRequest;
httpWebRequest = WebRequest.Create(url) as HttpWebRequest;
httpWebRequest.Method = "GET";
httpWebRequest.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8";
httpWebRequest.Headers["Cache-Control"] = "max-age=0";
httpWebRequest.ContentType = "application/x-www-form-urlencoded";
httpWebRequest.Referer = url;
httpWebRequest.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.181 Safari/537.36";
httpWebRequest.Proxy = null;
httpWebRequest.AllowAutoRedirect = true;
httpWebRequest.Credentials = CredentialCache.DefaultCredentials;
try
{
httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();
}
catch (WebException wex)
{
Console.WriteLine(wex.Message);
}
refcookies = ""; //out string parameter
foreach (Cookie ck in httpWebResponse.Cookies)
{
refcookies += ck.Name + "=" + ck.Value + "; ";
}
try
{
using (Stream inStream = httpWebResponse.GetResponseStream())
{
MemoryStream outStream = new MemoryStream();
byte[] buffer = new byte[1024];
int size = 0;
while ((size = inStream.Read(buffer, 0, (int)buffer.Length)) > 0)
{
outStream.Write(buffer, 0, size);
}
return outStream;
}
}
catch
{
if (httpWebResponse != null)
{
httpWebResponse.Close();
httpWebResponse = null;
}
return null;
//throw;
}
finally
{
httpWebRequest = null;
}
}
Here is a sample code I have from a button to store it in the picturebox:
string cookies;
Stream s;
s = Http.GetStream(codeUrl, out cooikes);
Image image = Image.FromStream(s); //returns an error here - System.ArgumentException: 'Parameter is not valid.'
byte[] data;
using (MemoryStream ms = new MemoryStream())
{
image.Save(ms, image.RawFormat);
ms.Position = 0;
data = new byte[ms.Length];
ms.Read(data, 0, Convert.ToInt32(ms.Length));
pictureBox2.Image = Image.FromStream(ms);
ms.Flush();
}
[Update]
Could this be a problem from the server of the 3rd party website?
- not a server issue, seems that the response value is a _incapsula_resource.
Thanks in advance.
The stream GetStream returns is not in the required format.
Check the httpWebResponse properties upon arrival, particularly the ContentType. It should be image/png or similar. Most probably the stream you receive from it is not ONLY the image binary but other values as well.
For unknown reason the server changed it response based from what I have set in the request headers.
These are the only request headers I managed to set to make it work the rest in the code you will find in the above question is removed:
httpWebRequest.Method = "GET";
httpWebRequest.Referer = url;
httpWebRequest.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.181 Safari/537.36";
So far now the response is successfully converted into stream and managed to store it within the picturebox. Both urls mentioned above in my question are working using only those headers.
Related
In my C# Winform application I'm trying to send form post data to a web page of Instagram:
try
{
HttpWebRequest req = (HttpWebRequest)WebRequest.Create("https://www.instagram.com/create/configure/");
req.Method = "POST";
if (req.CookieContainer == null)
req.CookieContainer = new CookieContainer();
foreach (System.Net.Cookie cookie in cookies)
{
req.CookieContainer.Add(cookie);
}
String postData = "upload_id=" + upload_id + "&caption=Wow&usertags=&custom_accessibility_caption=&retry_timeout=";
var data = Encoding.UTF8.GetBytes(postData);
req.ContentType = "application/x-www-form-urlencoded";
req.ContentLength = data.Length;
req.Accept = "*/*";
req.Referer = "https://www.instagram.com/create/details/";
req.Headers["accept-encoding"] = "gzip, deflate, br";
req.Headers["accept-language"] = "en-US,en;q=0.9";
req.Headers["origin"] = "https://www.instagram.com";
req.UserAgent = "Mozilla/5.0 (iPhone; CPU iPhone OS 10_0_1 like Mac OS X) AppleWebKit/602.1.50 (KHTML, like Gecko) Version/10.0 Mobile/14A403 Safari/602.1";
req.Headers["x-requested-with"] = "XMLHttpRequest";
req.Headers["x-ig-app-id"] = "1217981644879628";
req.Headers["x-csrftoken"] = csrftoken;
req.Headers["x-instagram-ajax"] = rolloutHash;
using (Stream requestStream = req.GetRequestStream())
{
requestStream.Write(data, 0, data.Length);
requestStream.Close();
}
string source;
using (StreamReader reader = new StreamReader(req.GetResponse().GetResponseStream()))
{
source = reader.ReadToEnd();
}
return source;
}
catch (Exception e)
{
int t = 0;
t++;
}
return "";
But I'm always getting 400 Exception from it. Any idea what can be the problem?
You are probably using an old version of api documentation, currently instagram api allows very limited actions.
check this
https://www.instagram.com/developer/endpoints/
and this
https://developers.facebook.com/docs/instagram-api/reference/media
I want to login to Rockstar Social Club page https://pl.socialclub.rockstargames.com
I have this script
public static void Login()
{
string firstUrl = "https://pl.socialclub.rockstargames.com/profile/signin";
string formParams = string.Format("login-field={0}&password-field={1}", "mynickname", "mypassword");
string cookieHeader;
WebRequest req = WebRequest.Create(firstUrl);
req.ContentType = "application/x-www-form-urlencoded";
req.Method = "POST";
byte[] bytes = Encoding.ASCII.GetBytes(formParams);
req.ContentLength = bytes.Length;
using (Stream os = req.GetRequestStream())
{
os.Write(bytes, 0, bytes.Length);
}
WebResponse resp = req.GetResponse();
cookieHeader = resp.Headers["Set-cookie"];
string pageSource;
string getUrl = "https://pl.socialclub.rockstargames.com/games/gtav/pc/career/overview/gtaonline";
WebRequest getRequest = WebRequest.Create(getUrl);
getRequest.Headers.Add("Cookie", cookieHeader);
WebResponse getResponse = getRequest.GetResponse(); //Here returns me this error: System.Net.WebException: 'An error occurred while sending the request"
using (StreamReader sr = new StreamReader(getResponse.GetResponseStream()))
{
pageSource = sr.ReadToEnd();
}
}
Error occures in WebResponse getResponse = getRequest.GetResponse();
System.Net.WebException: 'An error occurred while sending the request'
I don't know how to repair this, and succesfully login to this website.
I have accomplished what you are attempting to do, but on a different website.
Basically - a few years ago, I wanted to create a website that would track my Guild/Company details on Final Fantasy XIV.
They didn't have an API, so I made one.
In order to get the information I required, I needed to use a mix of HtmlAgilityPack along with the C# WebBrowser control.
In order to pass the verification token stage above, you need to run the page source in a Web Browser control.
This will allow dynamic fields and data to be generated.
You then need to take that data, and submit it with your post data.
This is to fool it into thinking the request is coming from the page.
Be warned, when doing your posts - you may need to allow for redirects and you may need to mirror the referrer and host fields to match the website you are emulating.
The specific process I followed was:
Navigate to login page in WebBrowser control
Get page source
Load into HtmlAgilityPack HtmlDocument class
Use XPath to scrape the login form.
Take _verification tokens, csrf tokens etc make note of them.
Post a web-request with the necessary data to the form target destination url.
Read the response
Be aware - sometimes the response will actually be html code that tells it to do a Javascript redirect - in my case with Final Fantasy XIV - it was loading up another form and performing an autopost on page load.
You will also want to use
LoggedInCookies = new CookieContainer();
In your first HttpWebRequest
followed by:
request.CookieContainer = LoggedInCookies;
for each subsequent request.
The cookie container will trap and persist the authentication related cookies, while the WebBrowser control and HtmlAgilityPack will allow you to scrape the fields from the web forms that you need to break through.
Adding some code from wayback when I solved this for Final Fantasy XIV's lodestone website.
This code is very old and may not work anymore, but the process it follows could be adapted for sites that do not use Javascript as part of the login process.
Pay attention to the areas where it allows the request to be redirected, this is because the Server endpoint you are calling may do Action redirects etc
If your request does not allow those redirects, then it will not be emulating the login process.
class LoggedInClient
{
public static CookieContainer LoginCookie(string user, string pass)
{
string sStored = "";
string url = "http://eu.finalfantasyxiv.com/lodestone/account/login/";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
CookieContainer cookies = new CookieContainer();
request.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.89 Safari/537.36";
request.CookieContainer = cookies;
HttpWebResponse response1 = (HttpWebResponse)request.GetResponse();
Console.WriteLine(cookies.Count.ToString());
string sPage = "";
using (var vPage = new StreamReader(response1.GetResponseStream()))
{
sPage = vPage.ReadToEnd();
}
HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
doc.LoadHtml(sPage);
sStored = doc.DocumentNode.SelectSingleNode("//input[#type='hidden' and #name='_STORED_']").Attributes["value"].Value;
string param = "sqexid="+user+"8&password="+pass+"&_STORED_=" + sStored;
string postURL = doc.DocumentNode.SelectSingleNode("//form[#name='mainForm']").Attributes["action"].Value;
//Console.WriteLine(sStored);
postURL = "https://secure.square-enix.com/oauth/oa/" + postURL;
request.Method = "POST";
byte[] paramAsBytes = Encoding.Default.GetBytes(param);
request = (HttpWebRequest)WebRequest.Create(postURL);
request.ContentType = "application/x-www-form-urlencoded";
request.Method = "POST";
request.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.89 Safari/537.36";
request.CookieContainer = cookies;
request.AllowAutoRedirect = false;
try
{
using (Stream stream = request.GetRequestStream())
{
stream.Write(paramAsBytes, 0, paramAsBytes.Length);
}
}
catch (Exception ee)
{
Console.WriteLine(ee.ToString());
}
string sGETPage = "";
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{
using (var vPage = new StreamReader(response.GetResponseStream()))
{
sPage = vPage.ReadToEnd();
sGETPage = response.Headers["Location"];
}
}
// Console.WriteLine(sPage);
request = (HttpWebRequest)WebRequest.Create(sGETPage);
request.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.89 Safari/537.36";
request.CookieContainer = cookies;
HttpWebResponse response2 = (HttpWebResponse)request.GetResponse();
Console.WriteLine(cookies.Count.ToString());
sPage = "";
using (var vPage = new StreamReader(response2.GetResponseStream()))
{
sPage = vPage.ReadToEnd();
}
// Console.WriteLine(sPage);
doc = new HtmlAgilityPack.HtmlDocument();
doc.LoadHtml(sPage);
string _c = doc.DocumentNode.SelectSingleNode("//input[#type='hidden' and #name='_c']").Attributes["value"].Value;
string cis_sessid = doc.DocumentNode.SelectSingleNode("//input[#type='hidden' and #name='cis_sessid']").Attributes["value"].Value;
string action = doc.DocumentNode.SelectSingleNode("//form[#name='mainForm']").Attributes["action"].Value;
string sParams = "_c=" + _c + "&cis_sessid=" + cis_sessid;
byte[] bData = Encoding.Default.GetBytes(sParams);
// Console.WriteLine(sStored);
request = (HttpWebRequest)WebRequest.Create(action);
request.ContentType = "application/x-www-form-urlencoded";
request.Method = "POST";
request.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.89 Safari/537.36";
request.CookieContainer = cookies;
request.AllowAutoRedirect = true;
try
{
using (Stream stream = request.GetRequestStream())
{
stream.Write(bData, 0, bData.Length);
}
}
catch (Exception ee)
{
Console.WriteLine(ee.ToString());
}
string nextPage = "";
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{
using (var vPage = new StreamReader(response.GetResponseStream()))
{
nextPage = vPage.ReadToEnd();
}
}
// Console.WriteLine(nextPage);
doc = new HtmlAgilityPack.HtmlDocument();
doc.LoadHtml(nextPage);
string csrf_token = doc.DocumentNode.SelectSingleNode("//input[#type='hidden' and #name='csrf_token']").Attributes["value"].Value;
string cicuid = "51624738";
string timestamp = Convert.ToInt32(DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1)).TotalSeconds).ToString() + "100";
action = "http://eu.finalfantasyxiv.com/lodestone/api/account/select_character/";
sParams = "csrf_token=" + csrf_token + "&cicuid=" + cicuid + "×tamp=" + timestamp;
bData = Encoding.Default.GetBytes(sParams);
request = (HttpWebRequest)WebRequest.Create(action);
request.ContentType = "application/x-www-form-urlencoded";
request.Method = "POST";
request.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.89 Safari/537.36";
request.CookieContainer = cookies;
request.AllowAutoRedirect = true;
try
{
using (Stream stream = request.GetRequestStream())
{
stream.Write(bData, 0, bData.Length);
}
}
catch (Exception ee)
{
Console.WriteLine(ee.ToString());
}
nextPage = "";
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{
using (var vPage = new StreamReader(response.GetResponseStream()))
{
nextPage = vPage.ReadToEnd();
}
}
return cookies;
}
}
Everything Working Properly but we click on submitData() button i will show me login page instead of myhomepage,
so very first I navigate login page it will set the cookie, then cookie stored in cookie container code is
CookieContainer cookieJar = new CookieContainer();
public Form1()
{
String captcha;
InitializeComponent();
}
private void BUTTON_LOGIN_Click(object sender, EventArgs e)
{
//Get captcha
var request = (HttpWebRequest)HttpWebRequest.Create("https://www.irctc.co.in/eticketing/loginHome.jsf");
request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8";
request.KeepAlive = true;
request.CookieContainer = cookieJar;
request.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36";
var response = request.GetResponse();
using (var stream = response.GetResponseStream())
{
}
// need array, different storage for every cookies
//
foreach (Cookie c in cookieJar.GetCookies(response.ResponseUri))
{
request.CookieContainer.Add(c);
MessageBox.Show("Cookie['" + c.Name + "']: " + c.Value);
}
}
Then I request for captcha using cookie
private void pictureBox1_Click(object sender, EventArgs e) // This Event Refresh thr captcha
{
//Get captch
var request =(HttpWebRequest)HttpWebRequest.Create("https://www.irctc.co.in/eticketing/captchaImage");
request.Accept = "image/webp,image/*,*/*;q=0.8";
WebHeaderCollection myWebHeaderCollection = request.Headers;
myWebHeaderCollection.Add("Accept-Language", "en-US;q=0.8");
myWebHeaderCollection.Add("Upgrade-Insecure-Requests", "1");
request.KeepAlive = true;
Uri target = new Uri("https://www.irctc.co.in");
cookieJar.Add(new Cookie("language", "en_IN") { Domain = target.Host });
request.CookieContainer = cookieJar;
request.Referer = "https://www.irctc.co.in/eticketing/loginHome.jsf";
request.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36";
var response = request.GetResponse();
using (var stream = response.GetResponseStream())
{
pictureBox1.Image = Bitmap.FromStream(stream);
}
// need array, different storage for every cookies
//
foreach (Cookie c in cookieJar.GetCookies(response.ResponseUri))
{
request.CookieContainer.Add(c);
MessageBox.Show("Cookie['" + c.Name + "']: " + c.Value);
}
}
private void button1_Click(object sender, EventArgs e)
{
submitData();
}
private void displayHtml(String html)
{
webBrowser1.Navigate("about:blank");
while (webBrowser1.Document == null || webBrowser1.Document.Body == null)
Application.DoEvents();
webBrowser1.Document.OpenNew(true).Write(html);
}
And finally I submit data after filling captcha to text box and here is error this is redirect me login page again so whats going wrong.
private void submitData()
{
try
{
String user = "myusername";
String pass = "mypassword";
String submit = "Submit";
String captcha=richTextBox1.Text.ToString() ;
ASCIIEncoding encoding = new ASCIIEncoding();
// MessageBox.Show(captcha);
string postData = "j_username=" + user + "&j_password=" + pass + "&j_captcha=" + captcha + "&submit=" + submit + "&tneg=" ;
byte[] data = Encoding.UTF8.GetBytes(postData);
Uri target = new Uri("https://www.irctc.co.in");
cookieJar.Add(new Cookie("language", "en_IN"){ Domain = target.Host });
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create("https://www.irctc.co.in/eticketing/home");
request.CookieContainer = cookieJar;
WebHeaderCollection myWebHeaderCollection = request.Headers;
myWebHeaderCollection.Add("Accept-Language", "en-US;q=0.8");
myWebHeaderCollection.Add("Upgrade-Insecure-Requests", "1");
request.Method = "POST";
request.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36";
request.ContentType = "application/x-www-form-urlencode";
request.Referer = "https://www.irctc.co.in/eticketing/loginHome.jsf";
request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8";
request.ServicePoint.Expect100Continue = false;
// var bytes = Encoding.UTF8.GetBytes("action=login&from=http%3A%2F%2Fwww.dofus.com%2Ffr&login=user123&password=password1232F&remember=1");
// request.ContentLength = bytes.Length;
request.ContentLength = data.Length;
request.KeepAlive = true;
// request.AutomaticDecompression = DecompressionMethods.GZip;
// request.AutomaticDecompression = DecompressionMethods.Deflate;
// request.Headers.Add(HttpRequestHeader.AcceptEncoding, "gzip, deflate, br");
// request.SendChunked = true;
// request.TransferEncoding = "gzip, deflate, br";
request.Host = "www.irctc.co.in";
//request.Headers.Add
Stream stream = request.GetRequestStream();
stream.Write(data, 0, data.Length);
stream.Close();
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
stream = response.GetResponseStream();
foreach (Cookie c in cookieJar.GetCookies(request.RequestUri))
{
MessageBox.Show("Cookie['" + c.Name + "']: " + c.Value);
}
StreamReader sr = new StreamReader(stream);
//richTextBox1.Text = sr.ReadToEnd();
String myhtml = sr.ReadToEnd().ToString();
displayHtml(myhtml);
sr.Close();
stream.Close();
}
catch (Exception ex)
{
MessageBox.Show("Error : " + ex.Message);
}
}
In the method submitData()
request.ContentType = "application/x-www-form-urlencode"
here is a typo, you have missed one 'd'
it should be like this :-
request.ContentType = "application/x-www-form-urlencoded"
ContentType is "application/x-www-form-urlencoded".
If the server is unable to understand the form data type(in this case "application/x-www-form-urlencode" is unknown to the server),then it will reject the request and redirects the user to login page as you mentioned.
You can also check Form ContentType in HTML Forms(w3.org) for more information.
Hope this helps :)
I am building one auto login application for one of my website. I have developed the auto login tool in C#. I have used httpwebrequest to make post call on the login page. I'm facing issue with the compression of the webpage. In some of the computers I'm not getting compressed response while in others I'm getting the response. Can anyone suggest how I can guaranteed get compressed response. The following is the code I have written for my httpwebrequest:
httpRequest.Connection = "keepalive";
httpRequest.Headers.Add(HttpRequestHeader.AcceptEncoding, "gzip, deflate, br");
httpRequest.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8";
httpRequest.UserAgent = "Mozilla/5.0 (Windows T 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.116 Safari/537.36";
httpRequest.UnsafeAuthenticatedConnectionSharing = true;
httpRequest.Headers.Set("Cache-Control", "no-cache");
httpRequest.Method = "POST";
byte[] bytes = Encoding.ASCII.GetBytes(postdata);
httpRequest.ContentLength = bytes.Length;
requestStream = httpRequest.GetRequestStream();
requestStream.Write(bytes, 0, bytes.Length);
requestStream.Flush();
requestStream.Close();
response = (HttpWebResponse)httpRequest.GetResponse();
HttpWebResponse response2 = response;
switch (response2.StatusCode)
{
case HttpStatusCode.OK:
responseStream = response2.GetResponseStream();
if (response2.ContentEncoding.ToLower().Contains("gzip"))
{
str = new StreamReader(new GZipStream(responseStream, CompressionMode.Decompress), Encoding.UTF8).ReadToEnd();
}
else if (response2.ContentEncoding.ToLower().Contains("deflate"))
{
str = new StreamReader(new DeflateStream(responseStream, CompressionMode.Decompress), Encoding.UTF8).ReadToEnd();
}
else
{
str = new StreamReader(responseStream, Encoding.UTF8).ReadToEnd();
}
responseStream.Close();
responseStream = null;
break;
}
I'm trying to download this json file to read out some data:
http://ddragon.leagueoflegends.com/cdn/5.7.2/data/en_US/item.json
My problem is now that somehow always the values in the tags attribute is missing.
I already used different methods to retrieve them used different encodings but nothing helped. Also the content length is less than the one which I receive in the browser.
Below are some codes I already tried. As you can see I used the WebClient to download this as a string. Also tried to download it as a file, same result.
Used HttpWebRequest with all header attributes.
Used the HEAD method to receive the content length which is less than in the browser.
I also tried using the MemoryStream and the StreamReader to get the correct data. Nothing worked.
String url = "http://ddragon.leagueoflegends.com/cdn/" + version + "/data/en_US/item.json";
//String json = new WebClient().DownloadString(url);
String json = "";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.UserAgent = "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.135 Safari/537.36";
request.ContentType = "text/plain; charset=utf-8 ";
request.Accept = "*/*";
request.Method = "GET";
//Get the headers associated with the request.
WebHeaderCollection myWebHeaderCollection = request.Headers;
myWebHeaderCollection.Add("DNT", "1");
myWebHeaderCollection.Add("Accept-Encoding", "gzip, deflate, sdch");
myWebHeaderCollection.Add("Accept-Language", "de-DE,de;q=0.8,en-US;q=0.6,en;q=0.4");
HttpWebRequest requestHeader = ((HttpWebRequest) WebRequest.Create(url));
requestHeader.Method = "HEAD";
using (WebResponse resp = requestHeader.GetResponse())
{
Console.WriteLine(resp.ContentLength);
}
Stream responseStream;
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
using (responseStream = response.GetResponseStream())
{
if (responseStream != null)
{
Console.WriteLine(response.ContentLength);
Console.WriteLine(response.ContentType);
StreamReader readStream = null;
if (string.IsNullOrEmpty(response.CharacterSet))
{
readStream = new StreamReader(responseStream);
}
else
{
readStream = new StreamReader(responseStream, Encoding.GetEncoding(response.CharacterSet));
}
json = readStream.ReadToEnd();
readStream.Close();
//using (var memoryStream = new MemoryStream())
//{
// responseStream.CopyTo(memoryStream);
// json = Encoding.UTF8.GetString(memoryStream.ToArray());
//}
}
}
Edit:
Sample Outputs:
Browser: http://pastebin.com/nQBj64L0
C# Client: http://pastebin.com/7a0Gxvcg