Hi i am trying to post to login page using tcpclient class . but it says 302 error. same thing i can do using webclient. please suggest where i am missing something. i have already achieved using webclient class. but i have asked to do at lower level. please suggest.
i have tried following
string sPost = "POST http://www.udupikrishnarestaurant.com/ HTTP/1.1\r\n" +
"Host: www.udupikrishnarestaurant.com\r\n" +
"Connection: keep-alive\r\n" +
"Content-Length: 632\r\n" +
"Cache-Control: max-age=0\r\n" +
"Origin: http://www.udupikrishnarestaurant.com \r\n" +
"Upgrade-Insecure-Requests: 1\r\n" +
"User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.99 Safari/537.36 \r\n" +
"Content-Type: application/x-www-form-urlencoded\r\n" +
"Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8 \r\n" +
"Referer: http://www.udupikrishnarestaurant.com/ \r\n" +
"Accept-Encoding: gzip, deflate, br\r\n" +
"Accept-Language: en-US,en;q=0.8\r\n" +
"\r\n" +
"__EVENTTARGET=&__EVENTARGUMENT=&__VIEWSTATE=/wEPDwUKLTM0ODY4NTA5Mg9kFgRmD2QWAgIBDxYCHgdWaXNpYmxlaGQCAQ8WAh4EVGV4dAUOMTgyLjY5LjEwOC4yMTFkGAEFHl9fQ29udHJvbHNSZXF1aXJlUG9zdEJhY2tLZXlfXxYCBQlidG5TdWJtaXQFCWJ0bkZvcmdvdCvBSicNzlFc1g4UTZeO96HhGK8T&__VIEWSTATEGENERATOR=CA0B0334&__EVENTVALIDATION=/wEdAAr0HIr+WBkxh8yUckvqwLD4LqRynaTFa/5Hs7sApa3/2aRDGrH080vTDzQFiILKSswLyPw09K59GBb2rr9Ouzg352npSE7sRexHKIcGFqd4MCl0Pj8zqX34hAcL3lPJ6TZjemWCTRgEB59HPczIGVNwdjb4xzpKucCqKjegIpvFhjzmltaUM7aEAN+g9cP/m13MQ92x/wmKgUDwu+2MEZnSA5KkNGEzQJYQmFWkBWmYqAdLpnM=&ddlFloor=2&txtUserName=test&txtPassword=1234&btnSubmit.x=15&btnSubmit.y=3";
byte[] buf = new byte[1024];
string header = sPost;
var client = new TcpClient();
client.Connect("182.50.132.48", 80);
// send request
var stream = client.GetStream();
var streamReader = new StreamReader(stream);
var streamWriter = new StreamWriter(stream);
streamWriter.Write(header);
streamWriter.Flush();
stream.Flush();
// get response
var response1 = streamReader.ReadLine();
302 is not an error. It is a request for redirection - the form tells you "go there instead", most likely as a result of your login attempt. WebClient handles the redirection, your client doesn't.
There's a lot of HTTP to implement. Unless this is purely an excercise, don't try to write your own HTTP client. Redirection is just the top of the iceberg.
Related
I'm developing an app for a school project which uses data from CoinGecko's free public API. The way I'm getting the data is with the HttpWebRequest class. At first, I was able to use the API fast, without any problems, but recently the requests have been getting stuck at the GetResponse() function for a long time. Even pinging the API takes about 30 seconds to complete. I've read a lot of posts regarding this problem, and none of the suggestions worked.
try
{
string uri = "https://api.coingecko.com/api/v3/ping";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
request.Method = "GET";
request.Accept =
"text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng," +
"*/*;q=0.8,application/signed-exchange;v=b3;q=0.9";
request.ContentType = "application/json";
request.Headers.Add("accept-encoding", "gzip, deflate, br");
request.Headers.Add("accept-language", "en-US,en;q=0.9");
request.Headers.Add("cache-control", "max-age=0");
request.Headers.Add("if-none-match", "W/\"c1f074e1bdf979ac5dc291f2c0acf9e4\"");
request.Headers.Add("sec-ch-ua", "\" Not A;Brand\"; v = \"99\", \"Chromium\"; v = \"96\", \"Google Chrome\"; v = \"96\"");
request.Headers.Add("sec-ch-ua-mobile", "?0");
request.Headers.Add("sec-ch-ua-platform", "\"Windows\"");
request.Headers.Add("sec-fetch-dest", "document");
request.Headers.Add("sec-fetch-mode", "navigate");
request.Headers.Add("sec-fetch-site", "none");
request.Headers.Add("sec-fetch-user", "?1");
request.Headers.Add("upgrade-insecure-requests", "1");
request.UserAgent =
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko)" +
" Chrome/97.0.4692.71 Safari/537.36 Edg/97.0.1072.55";
request.Proxy = null;
using HttpWebResponse response = (HttpWebResponse)request.GetResponse();
if (response.StatusCode != HttpStatusCode.OK && response.StatusCode != HttpStatusCode.Created)
{
throw new ApplicationException("REST client error code: "
+ response.StatusCode.ToString());
}
using Stream stream = response.GetResponseStream();
if (stream == null)
{
throw new ApplicationException("Error: Null response");
}
using StreamReader reader = new StreamReader(stream);
return reader.ReadToEnd();
}
catch (Exception ex)
{
return ex.ToString();
}
It works with the browser so I copied all the browser request headers and added them to the code but they didn't help. I have also tried: Increasing the number of connections, playing around with all the security protocol types, disabling my firewall. Could it be that the API is detecting and slowing down my requests since they're not "human"?
Set Windows to prioritise IPv4 by typing the following in the command line:
netsh interface ipv6 set prefixpolicy ::ffff:0:0/96 46 4
Source
I have an Authorization code which I need to pass in body with some header value when calling an api. When trying the same from postman its working fine but C# Webclient throwing 403 error.
Code below:-
public string GetResponse(string AuthCode)
{
string url = "https://example.com//openam/oauth2/access_token?grant_type=authorization_code&realm=/cbpgatqa";
Uri uri = new Uri(string.Format(url));
ASCIIEncoding encoding = new ASCIIEncoding();
string postData = "code=" + AuthCode + "&redirect_uri=" + "http://localhost:8080";
byte[] data = Encoding.GetEncoding("UTF-8").GetBytes(postData);
// Create the request
var httpWebRequest = (HttpWebRequest)WebRequest.Create(uri);
httpWebRequest.Proxy.Credentials = System.Net.CredentialCache.DefaultCredentials;
httpWebRequest.Headers.Add(HttpRequestHeader.Authorization, "Basic " + "MzE4OGQwYjQtZTRlOC00MTZjLTg5NjAtZDNlYWFhMmNjY2IxOkx3NiVBa0x4NWtPM01rJTJ5RWwxbW1jR0ZYZmhTQmk1NHhIRCpzNiUyVUd5WXN0MCNVbyNMNWQhcVlpZE93djc=");
httpWebRequest.ContentType = "application/json";
httpWebRequest.Method = "POST";
httpWebRequest.ContentLength = data.Length;
httpWebRequest.UserAgent = #"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.106 Safari/537.36";
Stream stream = httpWebRequest.GetRequestStream();
stream.Write(data, 0, data.Length);
stream.Close();
// Get the response
HttpWebResponse httpResponse = null;
try
{
httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
}
catch (Exception ex)
{
Console.WriteLine("Exception while getting resource " + ex.Message);
return null;
}
string result = null;
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
result = streamReader.ReadToEnd();
}
return result;
}
Postman Curl command:-
Generated from a curl request:
curl -X POST
'https://example.com//openam/oauth2/access_token?grant_type=authorization_code&realm=/cbpgatqa'
-H 'Authorization: Basic MzE4OGQwYjQtZTRlOC00MTZjLTg5NjAtZDNlYWFhMmNjY2IxOkx3NiVBa0x4NWtPM01rJTJ5RWwxbW1jR0ZYZmhTQmk1NHhIRCpzNiUyVUd5WXN0MCNVbyNMNWQhcVlpZE93djc='
-H 'Cache-Control: no-cache'
-H 'Content-Type: application/x-www-form-urlencoded'
-d 'code=93317468-7464-4804-b38a-43e13265c4ac&redirect_uri=http%3A%2F%2Flocalhost%3A8080%2F'
I am not able to figure it out the issue . Can anyone please help me
The Problem solved using RestSharp and passing correct headers value
var client = new RestClient("https://example.com/openam/oauth2/access_token?grant_type=authorization_code&realm=/cbpgatqa");
var request = new RestRequest(Method.POST);
request.AddHeader("Content-Type", "application/x-www-form-urlencoded");
request.AddHeader("Cache-Control", "no-cache");
request.AddHeader("Authorization", "Basic MzE4OGQwYjQtZTRlOC00MTZjLTg5NjAtZDNlYWFhMmNjY2IxOkx3NiVBa0x4NWtPM01rJTJ5RWwxbW1jR0ZYZmhTQmk1NHhIRCpzNiUyVUd5WXN0MCNVbyNMNWQhcVlpZE93djc=");
request.AddParameter("undefined", "code=" + AuthCode + "&redirect_uri=http%3A%2F%2Flocalhost%3A8080", ParameterType.RequestBody);
IRestResponse response = client.Execute(request);
using (var ms = new MemoryStream(Encoding.Unicode.GetBytes(response.Content)))
{
// Deserialization from JSON
DataContractJsonSerializer deserializer = new DataContractJsonSerializer(typeof(Token));
Token token = (Token)deserializer.ReadObject(ms);
return userinfo= GetuserInfo(token.id_token);
}
I too faced the same issue . Got it fixed after removing the parameter named "Host" . I am using third party rest API . they might have some restrictions .
I have an Exchange tenant with Microsoft (company.onmicrosoft.com), an admin account (admin#company.onmicrosoft.com), and the admin page through which I manage my (settings).
Recently, there is a project I took on to automatically parse some web data from the admin page via C#. I've read a lot of articles all relating to SharePoint and have read up on STS, SRF files, and active/passive federation services.
So, let's get to it:
I started off with https://login.microsoftonline.com/login.srf but with Fiddler realized I could just skip straight through to https://login.microsoftonline.com/ppsecure/post.srf and achieve the same result.
So, I go to login, enter my credentials, and it then forwards me the admin page. Simple enough, right?
So, I decided to replicate the network traffic and have relied on the following posts and examples:
http://allthatjs.com/2012/03/28/remote-authentication-in-sharepoint-online/
http://garymcallisteronline.blogspot.co.uk/2012/11/claims-explained.html
http://www.wictorwilen.se/post/how-to-do-active-authentication-to-office-365-and-sharepoint-online.aspx
http://blogs.msdn.com/b/cjohnson/archive/2011/05/03/authentication-with-sharepoint-online-and-the-client-side-object-model.aspx
http://blogs.msdn.com/b/cjohnson/archive/2011/05/14/part-2-headless-authentication-with-sharepoint-online-and-the-client-side-object-model.aspx
All these websites have great C# examples on screen scraped authentication, ADFS, and a bunch of other useful information. The problem is that all of it is for SharePoint.
So, I mixed and matched code and came up with the following:
static void Main3()
{
CookieContainer cookies = new CookieContainer();
//using Bungie example from http://stackoverflow.com/questions/2508656/logging-into-a-site-that-uses-live-com-authentication
//Uri url = new Uri("https://login.live.com/login.srf?wa=wsignin1.0&rpsnv=11&ct=1268167141&rver=5.5.4177.0&wp=LBI&wreply=http:%2F%2Fwww.bungie.net%2FDefault.aspx&id=42917");
Uri url = new Uri("https://portal.microsoftonline.com/");
HttpWebRequest http = (HttpWebRequest)HttpWebRequest.Create(url);
http.Timeout = 30000;
http.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:26.0) Gecko/20100101 Firefox/26.0";
http.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
http.AllowAutoRedirect = false;
http.Headers.Add("Accept-Language", "en-us,en;q=0.5");
http.Headers.Add("Accept-Encoding", "gzip, deflate"); //this option creates the two cookies but loads garbage HTML. Removing this option allows the HTML to load normally
http.KeepAlive = true;
http.CookieContainer = new CookieContainer();
http.Method = WebRequestMethods.Http.Get;
HttpWebResponse response = (HttpWebResponse)http.GetResponse();
//gets the cookies (they are set in the eighth header)
string[] strCookies = response.Headers.GetValues(8);
response.Close();
Cookie manualCookie;
string sManualCookiesString = "MSPRequ|lt=1389810702&id=271346&co=1;MSPOK|$uuid-02eeaf29-b8a5-441f-a6a6-319ed926d8bc$uuid-7f106156-1398-405f-83e5-61f177c7be25$uuid-3d2f189d-8f79-4216-97cf-23c5c22ff8ad$uuid-b93c9354-5802-4c82-ac7d-7838d2f7bdbc$uuid-071c3106-1c97-4e1e-930c-36f33b6f0b93; MSPShared|1; MSPSoftVis|#:#; MSPBack|1389810501";
//Manually insert the cookies since the request only returns two cookies and we need six cookies
foreach (string sCookieAndValue in sManualCookiesString.Split(';'))
{
string sCookieName = sCookieAndValue.Split('|')[0].Trim();
string sCookieValue = sCookieAndValue.Split('|')[1].Trim();
manualCookie = new Cookie(sCookieName, "\"" + sCookieValue + "\"");
Uri manualURL = new Uri("http://login.microsoftonline.com");
http.CookieContainer.Add(manualURL, manualCookie);
}
/* //Removed because the above request only returns MSPRequ and MSPOK cookies but leaves out the others. It's obviously broken :(
string name, value;
for (int i = 0; i < strCookies.Length; i++)
{
name = strCookies[i].Substring(0, strCookies[i].IndexOf("="));
value = strCookies[i].Substring(strCookies[i].IndexOf("=") + 1, strCookies[i].IndexOf(";") - strCookies[i].IndexOf("=") - 1);
manualCookie = new Cookie(name, "\"" + value + "\"");
Uri manualURL = new Uri("http://login.microsoftonline.com");
//http.CookieContainer.Add(manualURL, manualCookie);
}
*/
//stores the cookies to be used later
cookies = http.CookieContainer;
http = (HttpWebRequest)HttpWebRequest.Create(url);
response = (HttpWebResponse)http.GetResponse();
StreamReader readStream = new StreamReader(response.GetResponseStream());
string HTML = readStream.ReadToEnd();
readStream.Close();
//Get the PPSX value: these values are a bit strange and could indicate progress since I've seen a few of the following:
// P, Pa, Pas, Pass, Passp, Passpo, Passport, PassportRN
// As you can see it adds one letter at a time which could indicate where in the login process it is, but I don't know
string PPSX = HTML.Remove(0, HTML.IndexOf("PPSX"));
PPSX = PPSX.Remove(0, PPSX.IndexOf("value") + 7);
PPSX = PPSX.Substring(0, PPSX.IndexOf("\""));
//PPSX = "PassP"; //debug
//Get this random PPFT value
string PPFT = HTML.Remove(0, HTML.IndexOf("PPFT"));
PPFT = PPFT.Remove(0, PPFT.IndexOf("value") + 7);
PPFT = PPFT.Substring(0, PPFT.IndexOf("\""));
//Get the random URL you POST to
//string POSTURL = HTML.Remove(0, HTML.IndexOf("https://login.microsoftonline.com/ppsecure/post.srf?wa=wsignin1.0&rpsnv=2&ct="));
//POSTURL = POSTURL.Substring(0, POSTURL.IndexOf("\""));
//debug:
//based on Fiddler, this page is the next page that's loaded
string POSTURL = "https://login.microsoftonline.com/GetUserRealm.srf?login=admin%company.onmicrosoft.com&handler=1&extended=1 ";
//POST with cookies
HttpWebRequest http2 = (HttpWebRequest)HttpWebRequest.Create(POSTURL);
//http.AllowAutoRedirect = false;
http2.Accept = "application/json, text/javascript, */*; q=0.01";
http2.Headers.Add("Accept-Encoding", "gzip, deflate");
http2.Headers.Add("Accept-Language", "en-us,en;q=0.5");
http2.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:26.0) Gecko/20100101 Firefox/26.0";
//http.ContentLength = 0;
http2.KeepAlive = true;
http.CookieContainer = cookies;
http2.Referer = "https://login.microsoftonline.com/ppsecure/post.srf";
http2.Method = WebRequestMethods.Http.Post;
Stream ostream = http2.GetRequestStream();
//used to convert strings into bytes
System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();
//Post information found via Fiddler. Values have been altered for anonymity
byte[] buffer = encoding.GetBytes(
"login=" + sUsername
+ "&passwd=" + sPassword
+ "&PPSX=" + PPSX
+ "&PPFT=" + PPFT
+ "&n1=107313"
+ "&n2=-1389941230500"
+ "&n3=-1389941230500"
+ "&n4=112373"
+ "&n5=112373"
+ "&n6=112373"
+ "&n7=112373"
+ "&n8=NaN"
+ "&n9=112373"
+ "&n10=112360"
+ "&n11=112358"
+ "&n12=112323"
+ "&n13=112324"
+ "&n14=112396"
+ "&n15=26"
+ "&n16=11239"
+ "&n17=112369"
+ "&n18=112315"
+ "&n19=880.9711230112345"
+ "&n20=1"
+ "&n21=1"
+ "&n22=1381236981084.398"
+ "&n23=1"
+ "&n24=46.789501123103664"
+ "&n25=0"
+ "&n26=0"
+ "&n27=0"
+ "&n28=0"
+ "&n29=-1318912363023"
+ "&n30=-1318912363023"
+ "&n31=false"
+ "&n32=false"
+ "&type=11"
+ "&LoginOptions=3" //this is 2 sometimes
+ "&NewUser=1"
+ "&idsbho=1"
+ "&PwdPad="
+ "&sso="
+ "&vv="
+ "&uiver=1"
+ "&i12=1"
+ "&i13=Firefox"
+ "&i14=26.0"
+ "&i15=1480"
+ "&i16=964"
);
ostream.Write(buffer, 0, buffer.Length);
ostream.Close();
HttpWebResponse response2 = (HttpWebResponse)http.GetResponse();
readStream = new StreamReader(response2.GetResponseStream());
HTML = readStream.ReadToEnd();
response2.Close();
ostream.Dispose();
foreach (Cookie cookie in response2.Cookies) //this returns no cookies
{
Console.WriteLine(cookie.Name + ": ");
Console.WriteLine(cookie.Value);
Console.WriteLine(cookie.Expires);
Console.WriteLine();
}
POSTURL = "https://login.microsoftonline.com/ppsecure/post.srf?bk=1389198967";
//POST with cookies
http = (HttpWebRequest)HttpWebRequest.Create(POSTURL);
http.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:26.0) Gecko/20100101 Firefox/26.0";
http.AllowAutoRedirect = false;
http.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
http.Headers.Add("Accept-Language", "en-us,en;q=0.5");
http.KeepAlive = true;
http.CookieContainer = cookies;
http.Referer = "https://login.microsoftonline.com/ppsecure/post.srf";
http.Method = WebRequestMethods.Http.Post;
ostream = http.GetRequestStream();
//used to convert strings into bytes
encoding = new System.Text.ASCIIEncoding();
//Post information
ostream.Write(buffer, 0, buffer.Length);
ostream.Close();
HttpWebResponse response3 = (HttpWebResponse)http.GetResponse();
readStream = new StreamReader(response3.GetResponseStream());
HTML = readStream.ReadToEnd();
response3.Close();
ostream.Dispose();
foreach (Cookie cookie in response3.Cookies) //sadly this returns no cookies when it should have 18 cookies in addition to a bunch of BOX.CacheKey cookies
{
Console.WriteLine(cookie.Name + ": ");
Console.WriteLine(cookie.Value);
Console.WriteLine(cookie.Expires);
Console.WriteLine();
}
}
Does anyone know enough about how the website works to provide some guidance on where I'm failing? The website also uses JavaScript to create cookies (you can visit the first page and enter a random address and you'll see some dots move from left to right in the username field.
Maybe I'm taking the wrong approach but any assistance would be appreciated. I can provide the Fiddler summary session log if need be.
Thank you!
P.S. Sorry about all .com replacements for the actual period character but I don't have enough reputation points. Just search for [dot]com and replace with .com to undo.
Put a WebBrowser control on a form and control it using its methods and events - it will handle all the redirects/cookies/etc for you.
You can hide it if you need to so the user won't even know you're using a web browser...
You want to login into Office 365 portal, not SharePoint admin portal, so the articles about how to login into SharePoint won't help for your case.
I did a search, I think to login into Office 365 portal, you need Microsoft Online Services Sign-In Assistant first.
Microsoft provides some examples on how to manage office365 user:
Office 365: Office 365 Dashboard
Office 365: Manage licenses and subscriptions for Office 365
Both examples are based on Microsoft Online Services Sign-In Assistant and powershell cmdlet , so in C#, we can use the functions in this component to login. But it seems there's not much document about this component. So if you can use Powershell, you can follow the examples to do it. If you can't, you may need dig into the login command and see how to implement it in C#.
You will need the Modules for Office 365 installed in the machine for communication.
Powershell Commands
$UserCredential = Get-Credential
Import-PSSession $Session
Import-Module ActiveDirectory
Import-Module MsOnline
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $UserCredential -Authentication Basic -AllowRedirection
Import-PSSession $Session
Import-Module ActiveDirectory
Import-Module MsOnline
Get-Module
Then you can do any thing
Create user & mailboxes, Email alias etc.
You may have to reference power shell in the C# code to make it work work from a custom website or windows app
This will connect to Office 365:
http://technet.microsoft.com/en-us/library/jj984289(v=exchg.150).aspx
I have the following code:
string url = "https://myurl.com/is/here/example?param1=100¶m2=200";
string post = "POST " + url + " HTTP/1.1\r\n" +
"Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\r\n" +
"Accept-Encoding: gzip, deflate\r\n" +
"Accept-Language: en-US,en;q=0.5\r\n" +
"Connection: keep-alive\r\n" +
"Host: myurl.com\r\n" +
"User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:26.0) Gecko/20100101 Firefox/26.0\r\n" +
"Content-type: application/json\r\n" +
"X-HTTP-Method-Override: GET\r\n" +
"X-MY-CUSTOM-HEADER: headervalue\r\n\r\n";
TcpClient tcp = new TcpClient("myurl.com", 443);
string returnData = string.Empty;
using (SslStream stream = new SslStream(tcp.GetStream()))
{
//Authenticate here...
byte[] data = Encoding.ASCII.GetBytes(post);
stream.Write(data, 0, data.Length);
stream.Flush();
stream.ReadByte();
data = new byte[4096];
var n = stream.Read(data, 0, data.Length);
using (MemoryStream ms = new MemoryStream(data, 0, n))
{
ms.ReadByte();
ms.ReadByte();
using (DeflateStream df = new DeflateStream(ms, CompressionMode.Decompress))
using (StreamReader rd = new StreamReader(df))
{
returnData = rd.ReadToEnd();
}
}
}
tcp.Close();
However, the response is always empty. Is the post string correct? What am I missing here?
Edit:
I'm using SslStream and it seems to be retrieving data. However, Is there a way to just read till all data is received instead of
It appears that you're declaring the length of content to by the length of your URL (when expressed as UTF-8), rather than whatever is in the post body
If the content is not as long as the content-length header indicates, I would expect the remote server to wait for the rest of the content.
You're also connecting to port 443, and your URL starts 'https' but you don't appear to be making any attempt to do the SSL negotiation.
I have realy strange problem with Headers collection in WebClient class.
Here is my example:
WebClient client = new WebClient();
client.Headers.Add("User-Agent", "Mozilla/5.0 (Windows NT 6.2; WOW64; rv:23.0) Gecko/20100101 Firefox/23.0");
client.Headers.Add("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
client.Headers.Add("Accept-Language", "pl,en-us;q=0.7,en;q=0.3");
Console.WriteLine("Before request:");
foreach (string key in client.Headers)
{
Console.WriteLine(key + ": " + client.Headers[key]);
}
client.DownloadString("http://www.google.com");
Console.WriteLine();
Console.WriteLine("After request:");
foreach (string key in client.Headers)
{
Console.WriteLine(key + ": " + client.Headers[key]);
}
Console.ReadLine();
My result of running this simple program:
Before request:
User-Agent: Mozilla/5.0 (Windows NT 6.2; WOW64; rv:23.0) Gecko/20100101 Firefox/23.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: pl,en-us;q=0.7,en;q=0.3
After request:
Accept-Language: pl,en-us;q=0.7,en;q=0.3
Why my headers are disappearing?
because headers are sent and webclient did its job. if you want same headers for next request, you should add them again.