problem using proxy with HttpWebRequest in C# - c#

I'm using this code to use proxy with HttpWebRequest
public string GetBoardPageResponse(string url, string proxy = "")
{
ServicePointManager.Expect100Continue = false;
HttpWebRequest request = HttpWebRequest.Create(url) as HttpWebRequest;
HttpWebResponse response = null;
WebProxy myProxy = new WebProxy(proxy);
request.Proxy = myProxy;
request.Timeout = 20000;
request.ReadWriteTimeout = 20000;
request.Accept = "*/*";
request.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.3) Gecko/20100401 Firefox/3.6.3 (.NET CLR 3.5.30729)";
request.Headers.Add(HttpRequestHeader.AcceptEncoding, "gzip,deflate");
request.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
// SEND POST
Stream os = null;
StreamReader sr = null;
try
{
//post data
byte[] bytes = Encoding.ASCII.GetBytes(param);
if (param.Length > 0)
{
request.ContentLength = bytes.Length; //Count bytes to send
os = request.GetRequestStream();
os.Write(bytes, 0, bytes.Length); //Send it
}
// Get the response
HttpWebResponse webResponse;
using (webResponse = (HttpWebResponse)request.GetResponse())
if (webResponse == null)
return "";
sr = new StreamReader(webResponse.GetResponseStream(), Encoding.GetEncoding(webResponse.CharacterSet));
string encoding = webResponse.CharacterSet;
string data = sr.ReadToEnd().Trim();
return data;
}
catch (Exception ex)
{
return "";
}
finally
{
if (sr != null)
sr.Close();
if (response != null)
response.Close();
if (os != null)
os.Close();
}
}
now this function works fine if I don't use proxy server. but If I add any proxy it will return null result. if I use same proxy with WebClient it works like charm.. I really have no idea what's really blocking or bugging this..
any ideas or help will be appreciated!

just changed: using (webResponse = (HttpWebResponse)request.GetResponse())
to webResponse = (HttpWebResponse)request.GetResponse();
nooby miskate..

Related

Error submitting login form to external website through c#

I'm currently trying to login to a website and submit a form once logged into the website.
I've tried looking through all the input fields and adding it to the code but still no luck.
The website I'm trying to login to is http://sythe.org/login/
Here's what I currently have:
string formUrl = "http://www.sythe.org/login/";
string formParams = string.Format("login={0}&password={1}&_xfToken={2}&register={3}&remember={4}&cookie_check={5}&redirect={6}", "", "", "", "0", "1", "1", "https://sythe.org");
string cookieHeader;
WebRequest req = WebRequest.Create(formUrl);
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;
using (StreamReader sr = new StreamReader(resp.GetResponseStream()))
{
pageSource = sr.ReadToEnd();
}
Console.WriteLine(pageSource);
Thanks guys.
You can try this code which is generated using the C# plugin for Fiddler:
private void MakeRequests()
{
HttpWebResponse response;
if (Request_www_sythe_org(out response))
{
response.Close();
}
}
private bool Request_www_sythe_org(out HttpWebResponse response)
{
response = null;
try
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://www.sythe.org/login/login");
request.KeepAlive = true;
request.Headers.Set(HttpRequestHeader.CacheControl, "max-age=0");
request.Headers.Add("Origin", #"https://www.sythe.org");
request.Headers.Add("Upgrade-Insecure-Requests", #"1");
request.ContentType = "application/x-www-form-urlencoded";
request.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.86 Safari/537.36";
request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3";
request.Referer = "https://www.sythe.org/";
request.Headers.Set(HttpRequestHeader.AcceptEncoding, "gzip, deflate, br");
request.Headers.Set(HttpRequestHeader.AcceptLanguage, "en-GB,en-US;q=0.9,en;q=0.8");
request.Headers.Set(HttpRequestHeader.Cookie, #"__cfduid=de8826b5a5df064d6f2ccf2d0952ce52d1554285418; _ga=GA1.2.1744286790.1554285420; _gid=GA1.2.1835584013.1554285420; G_ENABLED_IDPS=google; sythe_bug_token=c0FFSCNAJio4MzJnZWEjWXlaV1NCempSdWFrUzRnUHVZUXJKOENicnkwSmZFTXROR1M3MDhUR1ovWTFIVmlvRFZaaVN2MjFHRWtaM2JhT0N1bTJHbEdGSG5VY1dUNjdmVGNZd1RoekNmTmgzaHB4T29OTTZkNytJQWh3cDdjeWlndWpidkJFY0NiRXF4b1ljSVJhN2VGVVd2eEsvK2hrak1pclhvYjRFeWRhT09UUU5seXZDUVlETDlYWUUyazZGajRub3VWV0k5aVhkVVJiWUtxcFRuaTljRElnWEVKd2o4T3ZSZlRheFEzRWRzekgxSXN4d0doMS9YRFR6L1d0OGRONUVLcDZHUEN3eVFRWEQ%3D; _gat=1; xf_session=63943f38ebba0827a4208d363cae5dcb");
request.Headers.Add("AlexaToolbar-ALX_NS_PH", #"AlexaToolbar/alx-4.0.3");
request.Method = "POST";
request.ServicePoint.Expect100Continue = false;
string body = #"login=testtest%40testtest.testtest&register=0&password=testtesttesttest&remember=1&cookie_check=1&redirect=%2F&_xfToken=";
byte[] postBytes = System.Text.Encoding.UTF8.GetBytes(body);
request.ContentLength = postBytes.Length;
Stream stream = request.GetRequestStream();
stream.Write(postBytes, 0, postBytes.Length);
stream.Close();
response = (HttpWebResponse)request.GetResponse();
}
catch (WebException e)
{
if (e.Status == WebExceptionStatus.ProtocolError) response = (HttpWebResponse)e.Response;
else return false;
}
catch (Exception)
{
if(response != null) response.Close();
return false;
}
return true;
}
try to replace string formUrl = "http://www.sythe.org/login/"; to string formUrl = "http://www.sythe.org/login/login";

using HtmlWeb causes HttpWebRequest to timeout

So I've got a situation where I'm using HtmlAgilityPack to load web pages in order to scrape the Document contents. I have a number of URLs that I need to load and a few of them require gzip encoding so I catch the exception thrown by HtmlWeb.load(), check that it's a gzip encoding issue, and then process the page load with HttpWebRequest. However this allows the first time through with HttpWebRequest to be successful, but any other attemp with HttpWebRequest will timeout.
Here's a cleaned up version of the code:
HtmlDocument doc = new HtmlDocument();
HtmlWeb web = new HtmlWeb();
try
{
doc = web.Load(uri);
Console.WriteLine("htmlweb and htmldocument success");
}
catch (ArgumentException ae)
{
Console.WriteLine("htmlweb and htmldocument not successful");
if (ae.Message.Contains("\'gzip\'"))
{
HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(uri);
try
{
req.Headers[HttpRequestHeader.AcceptEncoding] = "gzip, deflate";
req.AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip;
req.Method = "GET";
//req.UserAgent = "Mozilla/5.0 (Windows; U; MSIE 9.0; WIndows NT 9.0; en-US))";
string source;
req.KeepAlive = false;
//req.Timeout = 100000;
// On the second iteration we never get beyond this line
using (WebResponse webResponse = req.GetResponse())
{
using (HttpWebResponse httpWebResponse = webResponse as HttpWebResponse)
{
using (StreamReader reader = new StreamReader(httpWebResponse.GetResponseStream()))
{
source = reader.ReadToEnd();
}
}
}
req.Abort();
Console.WriteLine("httpwebresponse successfull");
}
catch (WebException we)
{
Console.WriteLine("httpwebresponse not successful");
}
}
}
Is there some cleanup that I'm needing to do? or is there something I'm forgetting?
Any help will be greatly appreciated.
I think that I will have to load via WebRequest first, instead of HtmlWeb. then inspect the response header for gzip, and decompress as needed each time.
System.Net.HttpWebRequest req = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(uri);
//req.Headers[HttpRequestHeader.AcceptEncoding] = "gzip, deflate";
//req.AutomaticDecompression = System.Net.DecompressionMethods.Deflate | System.Net.DecompressionMethods.GZip;
//req.Method = "GET";
string source = String.Empty;
try
{
using (System.Net.WebResponse webResponse = req.GetResponse())
{
using (HttpWebResponse httpWebResponse = webResponse as HttpWebResponse)
{
StreamReader reader;
if (httpWebResponse.ContentEncoding.ToLower().Contains("gzip"))
{
reader = new StreamReader(new GZipStream(httpWebResponse.GetResponseStream(), CompressionMode.Decompress));
}
else if (httpWebResponse.ContentEncoding.ToLower().Contains("deflate"))
{
reader = new StreamReader(new DeflateStream(httpWebResponse.GetResponseStream(), CompressionMode.Decompress));
}
else
{
reader = new StreamReader(httpWebResponse.GetResponseStream());
}
source = reader.ReadToEnd();
}
}
req.Abort();
}
catch(Exception ex){
//received a 404 Error - apparently one of my links is now dead...
}

Application hangs on GetRequestStream() after first request

I've googled and searched here. Some suggest that streams were not being close, others suggested that it's a connection limit with ServicePointManager.DefaultConnectionLimit being set to 1. However, none of these seem to work.
My problem is, when i use this for the first time, it works:
using (var stream = request.GetRequestStream())
{
var data = Encoding.UTF8.GetBytes(post.ToString());
stream.Write(data, 0, data.Length);
}
When I use it a second time, it freezes. Yes, I'm disposing my stream, yes im Aborting and closing my response and requests.
Here is my entire code segment:
public string get_hash(string strUsername, string strPassword, string strUniverse)
{
// Request VAR
var request = (HttpWebRequest)WebRequest.Create("http://website.com/");
// Response VAR
var response = (HttpWebResponse)request.GetResponse();
// Cookie Var
var cookie = new CookieContainer();
ServicePointManager.DefaultConnectionLimit = 100;
request.Timeout = 10;
request = (HttpWebRequest)WebRequest.Create("http://website.com/main/login");
request.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8";
request.Headers.Add("Accept-Encoding", "gzip,deflate,sdch");
request.Headers.Add("Accept-Language", "en-US,en,q=0.8");
request.Headers.Add("Cache-Control", "max-age=0");
request.ContentType = "application/x-www-form-urlencoded";
request.Host = "website.com";
request.Headers.Add("Origin", "http://website.com");
request.Referer = "http://website.com/";
request.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.117 Safari/537.36";
request.Method = "POST";
request.CookieContainer = new CookieContainer();
request.CookieContainer.Add(cookie.GetCookies(request.RequestUri));
// SET POST DATA HERE
var post = HttpUtility.ParseQueryString(string.Empty);
post.Add("uni", strUni);
post.Add("login", strUsername);
post.Add("pass", strPassword);
using (var stream = request.GetRequestStream())
{
var data = Encoding.UTF8.GetBytes(post.ToString());
stream.Write(data, 0, data.Length);
stream.Close();
stream.Dispose();
}
response = (HttpWebResponse)request.GetResponse();
string strSSID = "Failed";
if (response.StatusCode == HttpStatusCode.OK)
{
var data = string.Empty;
using (var sReader = new StreamReader(response.GetResponseStream()))
{
data = sReader.ReadToEnd();
sReader.Close();
sReader.Dispose();
}
string strSSIDurl = response.ResponseUri.ToString();
int intSSIDurlStart = strSSIDurl.IndexOf("PHPSESSID=") + 10;
strSSID = strSSIDurl.Substring(intSSIDurlStart);
}
request.Abort();
response.Close();
response.Dispose();
return strSSID;
}
you are not disposing the response from the first request.
var request = (HttpWebRequest)WebRequest.Create("http://website.com/");
var response = (HttpWebResponse)request.GetResponse(); // Not disposed
We had similar problem and disposing the response properly helped us.

getting response from Facebook is getting timed out

Hi I am using a facebook posts feeding application which will read all the new posts from my facebook page and will save to my sharepoint list. Earlier, i.e, before June it has worked properly it feeds all the posts from fabebook to my Share point list. But nowadays its throws an error while getting response from the Facebook authorize url. I don't know what went wrong. Please help me if you guys have any suggestion to resolve this issue. I am appending my code part below.
private void btnSendToList_Click(object sender, EventArgs e)
{
try
{
if (ConfigurationSettings.AppSettings["fbClientID"] != null)
strClientID = ConfigurationSettings.AppSettings["fbClientID"];
if (ConfigurationSettings.AppSettings["fbRedirectURL"] != null)
strURL = ConfigurationSettings.AppSettings["fbRedirectURL"];
if (ConfigurationSettings.AppSettings["fbCltSecret"] != null)
strCltSecret = ConfigurationSettings.AppSettings["fbCltSecret"];
if (ConfigurationSettings.AppSettings["fbUserId"] != null)
strUserId = ConfigurationSettings.AppSettings["fbUserId"];
if (ConfigurationSettings.AppSettings["SPSiteURL"] != null)
strSiteURL = ConfigurationSettings.AppSettings["SPSiteURL"];
CookieCollection cookies = new CookieCollection();
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://www.facebook.com");
request.UserAgent = "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/535.2 (KHTML, like Gecko) Chrome/15.0.874.121 Safari/535.2";
request.CookieContainer = new CookieContainer();
request.CookieContainer.Add(cookies);
//Get the response from the server and save the cookies from the first request..
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
cookies = response.Cookies;
string getUrl = "https://www.facebook.com/login.php?login_attempt=1";
string postData = String.Format("email={0}&pass={1}", "testuser#gmail.com", "test123$"); // Masking credentials.
getRequest = (HttpWebRequest)WebRequest.Create(getUrl);
getRequest.CookieContainer = request.CookieContainer;
getRequest.CookieContainer.Add(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";
byteArray = Encoding.ASCII.GetBytes(postData);
getRequest.ContentLength = byteArray.Length;
newStream = getRequest.GetRequestStream(); //open connection
newStream.Write(byteArray, 0, byteArray.Length); // Send the data.
getResponse = (HttpWebResponse)getRequest.GetResponse();
getRequest = (HttpWebRequest)WebRequest.Create(string.Format("https://graph.facebook.com/oauth/authorize?client_id={0}&redirect_uri={1}", strClientID, "http://www.facebook.com/connect/login_success.html"));
getRequest.Method = WebRequestMethods.Http.Get;
getRequest.CookieContainer = request.CookieContainer;
getRequest.CookieContainer.Add(cookies);
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";
getResponse = (HttpWebResponse)getRequest.GetResponse();
The above line throws WebExceptopn which is teeling like Process has timed out.
strAccessToken = getResponse.ResponseUri.Query;
strAccessToken = strAccessToken.Replace("#_=_", "");
strAccessToken = strAccessToken.Replace("?code=", "");
newStream.Close();
if (!string.IsNullOrEmpty(strAccessToken))
strCode = strAccessToken;
if (!string.IsNullOrEmpty(strCode) && !string.IsNullOrEmpty(strClientID) &&
!string.IsNullOrEmpty(strURL) && !string.IsNullOrEmpty(strCltSecret) &&
!string.IsNullOrEmpty(strUserId))
{
SaveToList();
}
}
catch (Exception ex)
{
LogError(ex);
}
}
Hi Finally i got the solution.
Here i am using getResponse = (HttpWebResponse)getRequest.GetResponse();
The problem is we can use only one HttpWebResponse at a time. In my case i am using the same object in two times without any disposing and closing. That's make me the error.
So I updated my code like this.
byteArray = Encoding.ASCII.GetBytes(postData);
getRequest.ContentLength = byteArray.Length;
newStream = getRequest.GetRequestStream(); //open connection
newStream.Write(byteArray, 0, byteArray.Length); // Send the data.
getResponse = (HttpWebResponse)getRequest.GetResponse();
getresponse.Close();
This solved my error. Thanks

Login to SSL page via HttpWebRequest

I am trying to access a SSL page via HttpWebRequest. It looks like the page uses JavaScript. I can get the login page, but after I login (I think its logged in), I cannot get the next page. Here is my code:
private void fetch(String password)
{
try
{
ServicePointManager.ServerCertificateValidationCallback = ((sender, certificate, chain, sslPolicyErrors) => true);
CookieContainer cookies = new CookieContainer();
HttpWebRequest http = (HttpWebRequest)WebRequest.Create("https://facebook.com/");
http.CookieContainer = cookies;
http.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0b; Windows NT 6.0)";
IWebProxy proxy = http.Proxy;
if (proxy != null)
{
Console.WriteLine("Proxy: {0}", proxy.GetProxy(http.RequestUri));
}
else
{
Console.WriteLine("Proxy is null; no proxy will be used");
}
WebProxy myProxy = new WebProxy();
Uri newUri = new Uri("myproxy");
myProxy.Address = newUri;
myProxy.Credentials = new NetworkCredential("username", password);
http.Proxy = myProxy;
HttpWebResponse response = (HttpWebResponse)http.GetResponse();
/////////////////////////////////////////////////////////
HttpStatusCode responseStatus;
responseStatus = response.StatusCode;
if (responseStatus == HttpStatusCode.OK)
{
UriBuilder urlBuilder = new UriBuilder("https://facebook.com/");
ServicePointManager.ServerCertificateValidationCallback = ((sender, certificate, chain, sslPolicyErrors) => true);
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(urlBuilder.ToString());
request.CookieContainer = cookies;
request.Referer = formUrl.ToString();
request.Method = "POST";
request.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0b; Windows NT 6.0)";
request.ContentType = "application/x-www-form-urlencoded";
request.AllowAutoRedirect = false;
using (Stream requestStream = request.GetRequestStream())
using (StreamWriter requestWriter = new StreamWriter(requestStream, Encoding.ASCII))
{
request.PreAuthenticate = true;
ICredentials credentials = new NetworkCredential("username", password);
request.Credentials = credentials;
request.Method = "POST";
}
string responseContent = null;
using (HttpWebResponse response2 = (HttpWebResponse)request.GetResponse())
{
using (Stream responseStream = response2.GetResponseStream())
{
using (StreamReader responseReader = new StreamReader(responseStream))
{
responseContent = responseReader.ReadToEnd();
}
Console.WriteLine(responseContent);
response.Close();
response2.Close();
}
}
}
else
{
Console.WriteLine("Client was unable to connect!");
}
}
catch (UriFormatException e)
{
Console.WriteLine("Invalid URL");
}
catch (IOException e)
{
Console.WriteLine("Could not connect to URL");
}
}

Categories