Why does HttpWebrequest through .Net proxy fail? - c#

I have the following code:
int repeat = 1;
int proxyIndex = 1;
if (listBox1.Items.Count == proxyIndex) //If we're at the end of the proxy list
{
proxyIndex = 0; //Make the selected item the first item in the list
}
try
{
int i = 0;
while (i < listBox1.Items.Count)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(textBox1.Text);
string proxy = listBox1.Items[i].ToString();
string[] proxyArray = proxy.Split(':');
WebProxy proxyz = new WebProxy(proxyArray[0], int.Parse(proxyArray[1]));
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
StreamReader reader = new StreamReader(response.GetResponseStream());
string str = reader.ReadToEnd();
Thread.Sleep(100);
{
repeat++;
continue;
}
}
catch (Exception ex) //Incase some exception happens
{
listBox2.Items.Add("Error:" + ex.Message);
}
I don't understand what I do wrong?

You're not setting Proxy on your HttpWebRequest. (You're creating a WebProxy object, but not using it.) You need to add:
request.Proxy = proxyz;
before calling request.GetResponse().

You also need to fix your use of objects which implement IDisposable. Since they're created in a loop, you cannot delay this - it could be causing any amount of random damage:
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
string[] proxyArray = proxyHostAndPort.Split(':');
WebProxy proxyz = new WebProxy(proxyArray[0], int.Parse(proxyArray[1]));
request.Proxy = proxyz;
using (HttpWebResponse response = (HttpWebResponse) request.GetResponse())
{
using (StreamReader reader = new StreamReader(response.GetResponseStream()))
{
string str = reader.ReadToEnd();
}
}

Related

HttpWebResponse().GetResponse Timed out

I'm trying to get my program to talk to an API using the HttpWebRequest/Response. The POST call works and generates an auth code.
The second call I'm making is a GET call and for some reason it times out every time. I've tried Request.Timeout, .readwritetimeout, content type, servicepoint and protocolVersion with no success.
This is my code:
public void InHouseReservations()
{
try
{
int Id = 0;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create($"{Properties.Settings.Default.RMSAPIIp}reservations/inHouse?modelType=basic&propertyId=1");
//request.Method = "GET";
request.ContentType = "application/json";
request.Headers.Add($"authtoken: {RMSAuthToken}");
request.Timeout = 200000;
request.ReadWriteTimeout = 200000;
request.ServicePoint.Expect100Continue = false;
request.ProtocolVersion = HttpVersion.Version11;
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{
Console.WriteLine(((HttpWebResponse)response).StatusDescription);
using (var dataStream = response.GetResponseStream())
{
StreamReader reader = new StreamReader(dataStream);
string responseFromServer = reader.ReadToEnd();
Console.WriteLine(responseFromServer);
}
var responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();
dynamic tokenJSON = JObject.Parse(responseString);
ReservationID = tokenJSON.Id;
response.Close();
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}

How do I reissue a webrequest? Must I recreate Webclient?

The following code gets stuck in a while loop when an exception is encountered. This is because GetResponse is caching the data.
According to fiddler, no data after the first request is issued.
Is it a best practice to recreate the webclient to solve the "refresh" issue I'm having?
private static ReportStatusEnum GetReportStatus(string domain, string oAuthKey, long permissionReportID)
{
string target = string.Format("https://{0}.egnyte.com/pubapi/v1/audit/jobs/{1}", domain, permissionReportID);
var client = new WebClient();
string result ="";
var request = (HttpWebRequest)WebRequest.Create(target);
request.ContentType = "application/json";
request.Headers.Add("Authorization", "Bearer " + oAuthKey);
request.AllowAutoRedirect = false;
bool callComplete = false;
while (callComplete != true)
{
try
{
using (var response = request.GetResponse())
{
using (StreamReader reader = new StreamReader(response.GetResponseStream()))
{
result = reader.ReadToEnd();
}
}
JToken result2 = JObject.Parse(result);
var statusResult = result2.SelectToken("status");
ReportStatusEnum ret = ReportStatusEnum.error;
Enum.TryParse<ReportStatusEnum>(statusResult.ToString(), out ret);
Console.WriteLine("The returned variable is:" + ret);
callComplete = true;
return ret;
}
catch (System.Net.WebException e)
{
if (e.Response != null)
if (e.Response.ContentLength > 0)
{
if (e.Response.Headers["X-Mashery-Error-Code"] == "ERR_403_DEVELOPER_OVER_QPS")
{
Thread.Sleep(60000); Console.Write("*QPS HIT*");
}
}
}
}
return ReportStatusEnum.error;
}
No. HttpWebRequests are not reusable.
Just move the creation of your WebRequest into the body of your loop:
string result ="";
bool callComplete = false;
while (callComplete != true)
{
var request = (HttpWebRequest)WebRequest.Create(target);
request.ContentType = "application/json";
request.Headers.Add("Authorization", "Bearer " + oAuthKey);
request.AllowAutoRedirect = false;
//...

How retry try statement in try/catch inside a foreach

Please see this thread:
In this thread we have the same issue answered about do...while. But what about foreach? Meaning how can we retry a try statement in try/catch inside this foreach with another proxy (another number of proxy_line_num integer):
foreach (string link_1 in links_with_kid)
{
try
{
getData = "";
req = (HttpWebRequest)WebRequest.Create(link_1);
req.Method = "GET";
req.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
req.UserAgent = "Mozilla/5.0 (Windows NT 6.1; rv:19.0) Gecko/20100101 Firefox/19.0";
req.ContentType = "text/html; charset=utf-8";
req.Referer = "http://www.example.com/";
req.KeepAlive = true;
req.Timeout = 25000;
if (useproxy)
{
string[] Ok_ip_port_ar = list_lines_GPL[proxy_line_num].Split(':');
proxy_line_num++;
if (proxy_line_num == list_lines_GPL.Count)
{
proxy_line_num = 0;
}
proxy = new WebProxy(Ok_ip_port_ar[0], int.Parse(Ok_ip_port_ar[1]));
}
req.Proxy = proxy;
req.CookieContainer = cookieJar1;
res = (HttpWebResponse)req.GetResponse();
Stream = res.GetResponseStream();
reader = new StreamReader(Stream);
reader_str = reader.ReadToEnd();
htmlDoc = new HtmlAgilityPack.HtmlDocument();
htmlDoc.LoadHtml(reader_str);
var images = from image in htmlDoc.DocumentNode.Descendants("img")
select image;
string address = string.Empty;
foreach (var image in images)
{
if (image.Attributes["src"] != null)
{
string[] address_ar = image.Attributes["src"].Value.Split('/');
string address_ar_last = address_ar[address_ar.Length - 1];
char[] address_ar_last_char = address_ar_last.ToCharArray();
if (address_ar_last_char.Length == 8
&&
Char.IsUpper(address_ar_last_char[0])
&&
Char.IsUpper(address_ar_last_char[1])
&&
Char.IsUpper(address_ar_last_char[2])
&&
Char.IsUpper(address_ar_last_char[3]))
{
address = image.Attributes["src"].Value;
string localFilename = #"c:\images-from-istgah\" + address_ar_last;
using (WebClient client = new WebClient())
{
client.DownloadFile(address, localFilename);
}
}
}
}
}
catch (Exception ex)
{
}
reader.Close();
Stream.Close();
res.Close();
}
I don't think that you can with a foreach. It is designed to give you the next item in the iterator.
If I were you I would use an ordinary for-loop with an iterator. That way you can control when to go to the next item.
Edit:
When writing it, a while actually made more sense in C#.
IEnumerator<String> iter = list.GetEnumerator();
bool bHasMore = iter.MoveNext();
while (bHasMore) {
try {
...
bHasMore = Iter.MoveNext();
}
catch ...
}
I dont have the entire refernce in my head, so you might need to look something up to get it to compile, but I hope not.

I keep receiving the script say "window.location.reload()" when use httpwebrequest

<!--#R--><script>
var k ='c991130d';
var d = new Date();
d.setTime(d.getTime() + (3600*24*365*5*1000));
document.cookie = "p7c=" + k +";
expires=" + d.toGMTString();
setTimeout(function(){
window.location.reload(); },2000);
</script>
please wait....
when I use my code to get a web page I keep getting these, but I can use XMLhttp with VB to get the whole page, it's my code below:
public static string GetPage(string Url)
{
HttpWebRequest WebRequest = (HttpWebRequest)HttpWebRequest.Create(Url);
WebRequest.Timeout = 5000;
WebRequest.AllowAutoRedirect = true;
WebRequest.Headers.Set("Pragma", "no-cache");
try
{
using (HttpWebResponse response = (HttpWebResponse)WebRequest.GetResponse())
{
using (Stream stream = response.GetResponseStream())
{
using (StreamReader sr = new StreamReader(stream, Encoding.GetEncoding("UTF-8")))
{ return sr.ReadToEnd(); }
}
}
}
catch (Exception ex){ return "%ERROR%" + ex.Message; }
}

Multitasking checking proxies

I wrote that little program:
string sciezka = "http://proxyjudge.hell-spy.de/";
foreach(var ip in listBox1.Items)
{
////////////////// CHANGES IP:PORT TO WEBPROXY HOST,PORT
string host=null;
string zmiana=null;
string sport = null;
int port=0;
int pozycja=0;
zmiana=ip.ToString();
pozycja=zmiana.IndexOf(":");
host=zmiana.Remove(pozycja);
sport = zmiana.Replace(host + ":", "");
port = int.Parse(sport);
////////////////////////////////////////// CONNECTING TO PROXYJUDGE
string anonymous=null;
try
{
WebRequest request = WebRequest.Create(sciezka);
WebProxy myprox = new WebProxy(host, port);
request.Timeout = 5000;
request.Proxy = myprox;
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream strumien = response.GetResponseStream();
StreamReader sr = new StreamReader(strumien);
anonymous = sr.ReadToEnd();
if (anonymous.Contains("HTTP_VIA"))
{
listBox3.Items.Add(zmiana);
}
else
{
listBox2.Items.Add(zmiana);
}
Update();
request.Abort();
sr.Close();
}
catch (Exception ex)
{
listBox3.Items.Add(zmiana);
Update();
}
and I want that it check few proxies at the same time... not one by one :)
can someone help with that?
well, you could use the .AsParallel extension method.
change
foreach(var ip in listBox1.Items)
to
foreach(var ip in listBox1.Items.AsParallel())

Categories