HttpWebResponse - c#

I want to download images from the server. When the image doesn't exist, I want to show my default image.
Here's my code:
string url = "http://www......d_common_conference" + "/" + c.id_common_conference + "-MDC.jpg";
try {
HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
request.Method = "HEAD";
HttpWebResponse response = request.GetResponse() as HttpWebResponse;
string status = Response.StatusCode.ToString();
img.ImageUrl = url;
}
catch (Exception excep) {
img.ImageUrl = "images/silhouete.jpg";
string msg = excep.Message;
}
It works nice, but until the 24th loop, no response, no exception thrown, and my program becomes jammed.
How can I fix this?

You aren't disposing of the HttpWebResponse, try this instead:
HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
request.Method = "HEAD";
string status;
using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
{
status = response.StatusCode.ToString();
}
I suspect you've hit the limit on TCP connections your machine will make (can't remember the number, but it's per CPU if memory serves)
p.s. there was a typo in your example, you weren't using the response variable from your WebRequest, but the Response object for the current request.

Related

Getting Unauthorized error on c# WebResponse response = request.GetResponse();

Im getting anauthorized error when doing a GET request on C# but if I try it using post man its returns just fine.
heres my c# code:
url = #"http://somesource.com/api/v10/" + url;
WebRequest request = WebRequest.Create(url);
request.Headers.Add("Authorization", "Token 3e68409924cc57ff07a8e29a18341fd99d3fba91ds");
request.Method = "GET";
request.Timeout = TimeO;
try {
WebResponse response = request.GetResponse();
status = ((HttpWebResponse)response).StatusDescription.ToString();
if (status != "OK")
Log.WriteLog(module, "Response x:: " + status);
Stream dataStream = response.GetResponseStream();
StreamReader reader = new StreamReader(dataStream);
dataResponse = reader.ReadToEnd();
reader.Close();
dataStream.Close();
response.Close();
} catch (WebException ex)
{
dataResponse = "{\"Error\":true,\"Update\":false,\"Msg\":\"RQ x:: " + ex.Message + "\"}";
Log.WriteLog(module, dataResponse);
}
it returns The remote server returned an error: (401) Unauthorized.
but when I try using the same url + header with Authorization = "Token 3e68409924cc57ff07a8e29a18341fd99d3fba91ds" on post man as GET request, it returns json data just fine.
although if I dont send the headers on postman i get
{
"detail": "Authentication credentials were not provided."
}
and if I intentionally set the token wrong, this is what I get:
{
"detail": "Invalid token."
}
different from what the c# program logs. which is
The remote server returned an error: (401) Unauthorized.
what could be the reason for this?
Thanks!
try the following code. original reference here
string url = #"https://telematicoprova.agenziadogane.it/TelematicoServiziDiUtilitaWeb/ServiziDiUtilitaAutServlet?UC=22&SC=1&ST=2";
WebRequest request = WebRequest.Create(url);
request.Credentials = GetCredential();
request.PreAuthenticate = true;
private CredentialCache GetCredential()
{
string url = #"https://telematicoprova.agenziadogane.it/TelematicoServiziDiUtilitaWeb/ServiziDiUtilitaAutServlet?UC=22&SC=1&ST=2";
ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3;
CredentialCache credentialCache = new CredentialCache();
credentialCache.Add(new System.Uri(url), "Basic", new NetworkCredential(ConfigurationManager.AppSettings["ead_username"], ConfigurationManager.AppSettings["ead_password"]));
return credentialCache;
}

HttpWebRequest.GetResponse() hangs randomly

I have the following code :
public static QHttpResponse Execute(QHttpRequest request)
{
//Setup the request
HttpWebRequest webrequest = (HttpWebRequest) WebRequest.Create(request.GetFinalUrl());
webrequest.AllowAutoRedirect = request.IsAllowRedirects;
webrequest.Method = request.Method;
webrequest.Accept = "application/json, text/javascript;q=0.9, */*;q=0.5";
webrequest.ContentType = "application/x-www-form-urlencoded; charset=UTF-8";
//request.Host is set automatically
webrequest.UserAgent = request.UserAgent;
if (!String.IsNullOrEmpty(request.Referrer))
webrequest.Referer = request.Referrer;
webrequest.Timeout = 50000;
webrequest.KeepAlive = false;
webrequest.CachePolicy = new HttpRequestCachePolicy(HttpRequestCacheLevel.Revalidate);
webrequest.AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip;
if (request.IsAjax)
{
webrequest.Headers.Add("X-Request", "JSON");
webrequest.Headers.Add("X-Requested-With", "XMLHttpRequest");
webrequest.Headers.Add("X-Prototype-Version", "1.7");
}
// Cookies
webrequest.CookieContainer = request.Cookies;
// Write the data to the body for POST and other methods
if (request.IsMethodPost())
{
byte[] dataBytes = Encoding.UTF8.GetBytes(request.GetDataParamString());
webrequest.ContentLength = dataBytes.Length;
using (Stream requestStream = webrequest.GetRequestStream())
requestStream.Write(dataBytes, 0, dataBytes.Length);
}
// Get the response
HttpWebResponse webresponse;
try
{
webresponse = webrequest.GetResponse() as HttpWebResponse;
}
catch (WebException wex)
{
if(request.IsBypassError)
webresponse = wex.Response as HttpWebResponse;
else
throw;
}
// Read to QHttpResponse object
QHttpResponse response = new QHttpResponse();
response.StatusCode = webresponse.StatusCode;
response.NewCookies = webresponse.Cookies;
using (Stream responseStream = webresponse.GetResponseStream())
using (StreamReader reader = new StreamReader(responseStream))
response.Reply = reader.ReadToEnd();
webresponse.Close();
return response;
}
I have this code run multiple times from various locations and randomly (Every couple of hours), it hangs at this line :
webresponse = webrequest.GetResponse() as HttpWebResponse;
I tried setting webrequest.KeepAlive = false;, but I continued to receive the error.
I'd like any available help on solving this, thanks in advance.
EDIT : I'd like to add that this code is executed from two threads. Occasionally they may connect to the same host, but only from these 2 threads. Also, as I see, the response is closed appropriately.
EDIT 2 : Visual studio's debugger says the execution is really stuck at System.dll!System.Net.Sockets.Socket.Receive.
EDIT 3 : In an attempt to see exactly what was causing the bug, I modified the "Get the response" code from above to
// Get the response
HttpWebResponse webresponse = null;
try
{
webresponse = webrequest.GetResponse() as HttpWebResponse;
}
catch (WebException wex)
{
Console.WriteLine("Time : " + DateTime.Now);
Console.WriteLine("Thread name : " + Thread.CurrentThread.Name);
Console.WriteLine("Exception : " + wex);
Console.WriteLine("Exc msg : " + wex.Message);
Console.WriteLine("Url : " + request.GetFinalUrl());
if (request.IsBypassError)
webresponse = wex.Response as HttpWebResponse;
else
{
if (webresponse != null)
{
webresponse.Close();
webresponse.Dispose();
}
throw;
}
}
I received this output :
Time : 5/11/2015 3:13:35 AM
Thread name : BOT A
Exception : System.Net.WebException: The remote server returned an error: (500) Internal Server Error.
at System.Net.HttpWebRequest.GetResponse()
at Gameloop.Util.Web.QWebClient.Execute(QHttpRequest request) in e:\Visual Studio - Workspace\Gameloop.Util\Gameloop.Util\Web\QWebClient.cs:line 52
Exc msg : The remote server returned an error: (500) Internal Server Error.
Url : https://website1.com/url/path/to/something (I changed this)
This was the only displayed error and was encountered by thread "BOT A". However, this was not the url the threads appear to have actually frozen at. "BOT A" was actually frozen at 12:00pm at website2.com and "BOT B" was actually frozen at 7:00am at website3.com. I doubt the hanging has much to do with the exception since the requests would have been made a large number of times after that before the actual hang.
My first inclination is that you may need to dispose of your HttpWebResponse. Normally you might wrap that in a using block, but since you have two places where webresponse might be assigned, you might just want to dispose it explicitly, like this.
webresponse.Close();
webresponse.Dispose();
I would start there.
Just add below in the try block:
httpWReq.Timeout = 3000;

scrape site after login

I'm trying to scrape a website that requires a login. Getting an error that I haven't received before, copied the code from another forum successfully in the past:
Exception Details: System.Net.ProtocolViolationException: Cannot send a content-body with this verb-type.
with the code:
Stream newStream = http.GetRequestStream(); //open connection
Here's the entire code:
#{
var strUserId = "userName";
var strPassword = "password";
var url = "formSubmitLandingSite";
var url2 = "pageToScrape";
HttpWebRequest http = WebRequest.Create(url) as HttpWebRequest;
http.KeepAlive = true;
http.Method = "POST";
http.ContentType = "application/x-www-form-urlencoded";
string postData = "email=" + strUserId + "&password=" + strPassword;
byte[] dataBytes = UTF8Encoding.UTF8.GetBytes(postData);
http.ContentLength = dataBytes.Length;
using (Stream postStream = http.GetRequestStream())
{
postStream.Write(dataBytes, 0, dataBytes.Length);
}
HttpWebResponse httpResponse = http.GetResponse() as HttpWebResponse;
// Probably want to inspect the http.Headers here first
http = WebRequest.Create(url2) as HttpWebRequest;
http.CookieContainer = new CookieContainer();
http.CookieContainer.Add(httpResponse.Cookies);
HttpWebResponse httpResponse2 = http.GetResponse() as HttpWebResponse;
Stream newStream = http.GetRequestStream(); //open connection
newStream.Write(dataBytes, 0, dataBytes.Length); // Send the data.
newStream.Close();
string sourceCode;
HttpWebResponse getResponse = (HttpWebResponse)http.GetResponse();
using (StreamReader sr = new StreamReader(getResponse.GetResponseStream()))
{
sourceCode = sr.ReadToEnd();
}
Response.Write(sourceCode);
}
You're creating a new request object here:
http = WebRequest.Create(url2) as HttpWebRequest;
Keep in mind that the default HTTP verb used is GET. Then you try to open the request stream here:
Stream newStream = http.GetRequestStream();
This method is used to enable writing data to the request's content. However, GET requests don't have content. As you do in the code above the error, you'll need to use a different HTTP verb. POST is most common for this, and is what you're using above:
http.Method = "POST";
So just use a POST request again. (Assuming, of course, that's what the server is expecting. In any event, if the server is expecting content then it's definitely not expecting a GET request.)

C# How can I get server error from a URL?

We have a url and we need to check whether web page is active or not. We tried following code:
WebResponse objResponse = null;
WebRequest objRequest = HttpWebRequest.Create(URL);
objRequest.Method = "HEAD";
try
{
objResponse = objRequest.GetResponse();
objResponse.Close();
}
catch (Exception ex)
{
}
Above code gave exception if unable to get a response but also works fine even if we have a "server error" on that page? Any help how to get server error?
The HttpResponse class has a StatusCode property which you can check. If it's 200 everything is ok.
You can change your code to this:
HttpWebResponse objResponse = null;
var objRequest = HttpWebRequest.Create("http://google.com");
objResponse = (HttpWebResponse) objRequest.GetResponse();
if(objResponse.StatusCode != HttpStatusCode.OK)
{
Console.WriteLine("It failed");
}else{
Console.WriteLine("It worked");
}
For one thing, use a using statement on the response - that way you'll dispose of it whatever happens.
Now, if a WebException is thrown, you can catch that and look at WebException.Response to find out the status code and any data sent back:
WebRequest request = WebRequest.Create(URL);
request.Method = "HEAD";
try
{
using (WebResponse response = request.GetResponse())
{
// Use data for success case
}
}
catch (WebException ex)
{
HttpWebResponse errorResponse = (HttpWebResponse) ex.Response;
HttpStatusCode status = errorResponse.StatusCode;
// etc
}

How to mix compression and caching in HttpWebRequest?

I have a c# client talking to a cherrypy(http/rest) webservice.
The problem is i can't both turn on compression and caching at the same time.
request.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
By leaving out the above line I get the correct caching headers (If-None-Math,If-Modified-Since) while commenting it out gets me the compression headers (Accept-Encodig:gzip) but not the caching headers. It seems to me like a bug but maybe i'm doing something wrong.
[full code]
public static string GET(string URL)
{
string JSON;
// Create the web request
HttpWebRequest request = WebRequest.Create(URL) as HttpWebRequest;
HttpRequestCachePolicy cPolicy = new HttpRequestCachePolicy(HttpRequestCacheLevel.Revalidate);
request.Accept = "application/json";
request.CachePolicy = cPolicy;
request.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
request.Pipelined = false;
// Get response
using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
{
// Get the response stream
StreamReader readerF = new StreamReader(response.GetResponseStream());
JSON = readerF.ReadToEnd();
// Console application output
//Console.WriteLine(JSON);
if (response.IsFromCache )
Console.WriteLine("Request not from cache");
}
return JSON;
}
I have implemented a workaround, see code below. I judged handling the compression easier than handling the cacheing so I implemented the compression part myself. Quite easy thanks to a blog post: HttpWebRequest and GZip Http Responses; I still think this is a bug in .net.
public static string GET(string URL)
{
string JSON;
// Create the web request
HttpWebRequest request = WebRequest.Create(URL) as HttpWebRequest;
HttpRequestCachePolicy cPolicy = new HttpRequestCachePolicy(HttpRequestCacheLevel.Revalidate);
request.Accept = "application/json";
request.Headers.Add(HttpRequestHeader.AcceptEncoding, "gzip,deflate");
request.CachePolicy = cPolicy;
request.Pipelined = false;
// Get response
using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
{
//From http://www.west-wind.com/WebLog/posts/102969.aspx
Stream responseStream = responseStream = response.GetResponseStream();
if (response.ContentEncoding.ToLower().Contains("gzip"))
responseStream = new GZipStream(responseStream, CompressionMode.Decompress);
else if (response.ContentEncoding.ToLower().Contains("deflate"))
responseStream = new DeflateStream(responseStream, CompressionMode.Decompress);
// Get the response stream
StreamReader readerF = new StreamReader(responseStream);
JSON = readerF.ReadToEnd();
}
return JSON;
}
Is this a side effect of the policy?
What happens if you just use the default policy, or other policies?
Other option is to manage the cache yourself.

Categories

Resources