C# - Getting response from WebResponse - c#

I'm trying to download XML source code of RSS feed placed in the internet, but when my process reaches WebResponse statement (second line bellow) process stops and nothing next is happening. There si no error, no exception or nothing like that. I'm waiting for tens of minutes and still nothing happening.
WebRequest request = WebRequest.Create(source.Url);
WebResponse response = await request.GetResponseAsync(); // at this line it stops
Stream receiveStream = response.GetResponseStream();
StreamReader readStream = new StreamReader(receiveStream);
string xml = readStream.ReadToEnd().Trim();
readStream.Dispose();
response.Dispose();
Any idea what causing it?
EDIT:
public static async Task<string> GetContent(string uri)
{
WebRequest request = WebRequest.Create(url);
using (WebResponse response = await request.GetResponseAsync().ConfigureAwait(false))
{
Stream receiveStream = response.GetResponseStream();
StreamReader readStream = new StreamReader(receiveStream);
return readStream.ReadToEnd().Trim();
}
}
public async Task<ObservableCollection<Source>> GetArticlesFromSource()
{
sourceDefinitions = await GetSourceDefinitions();
string imageFolderName = "ArticleImages";
string imageFolderPath = localFolder.Path + "\\" + imageFolderName;
StorageFolder imageFolder = await localFolder.CreateFolderAsync(imageFolderName, CreationCollisionOption.ReplaceExisting);
foreach (var source in sourceDefinitions)
{
if (source.Selected == "true")
{
ObservableCollection<Article> articlesStep1 = new ObservableCollection<Article>();
/*WebRequest request = WebRequest.Create(source.Url);
WebResponse response = await request.GetResponseAsync();
Stream receiveStream = response.GetResponseStream();
StreamReader readStream = new StreamReader(receiveStream);*/
string xml = await GetContent("http://www.honzachalupa.cz/").ConfigureAwait(false);
Debug.WriteLine(xml);
...

Your application is almost certainly calling Wait or Result on a returned task further up the call stack, and this will cause a deadlock that I explain in full on my blog.
To fix it, find the upstream Wait/Result call and change it to await. In other words, use "async all the way".

Related

How to view the body returned by an http 500 from a System.Net.HttpWebRequest

I've got a web service that returns an http 500 with some diagnostic information in the body of the response.
I'm doing something like
Stream responseStream = null;
WebResponse _Response = null;
Stream responseStream = null;
HttpWebRequest _Request = null;
try
{
_Response = _Request.GetResponse();
responseStream = _Response.GetResponseStream();
}
catch {
//try to view the Request.GetResponse() body here.
}
Since _Request.GetResponse() is returning an http 500 there doesn't seem to be a way to view the response body. According to HTTP 500 Response with Body? this was a known issue in Java 9 years ago. I'm wondering if there's a way to do it in .NET today.
The microsoft docs give a good run down of what HttpWebRequest.GetResponse returns if it fails, you can check it out here https://learn.microsoft.com/en-us/dotnet/api/system.net.httpwebrequest.getresponse?view=netframework-4.8
In your example I believe you need to check for WebException and handle it.
Stream responseStream = null;
WebResponse _Response = null;
Stream responseStream = null;
HttpWebRequest _Request = null;
try
{
_Response = _Request.GetResponse();
responseStream = _Response.GetResponseStream();
}
catch (WebException w)
{
//here you can check the reason for the web exception
WebResponse res = w.Response;
using (Stream s = res.GetResponseStream())
{
StreamReader r= new StreamReader(s);
string exceptionMessage = r.ReadToEnd(); //here is your error info
}
}
catch {
//any other exception
}

How to download youtube videos in a universal app?

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(videoInfoUrl);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream responseStream = response.GetResponseStream();
StreamReader reader = new StreamReader(responseStream, Encoding.GetEncoding("utf-8"));
string videoInfo = HttpUtility.HtmlDecode(reader.ReadToEnd());
NameValueCollection videoParams = HttpUtility.ParseQueryString(videoInfo);
My code:
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(videoInforUrl);
// HttpWebResponse response = await (HttpWebResponse)request.GetResponseAsync();
//Stream responseStream = response.GetResponseStream();
//StreamReader reader = new StreamReader(responseStream, Encoding.GetEncoding("utf-8"));
//string videoInfor=HttpUtility
I got issues from HttpWebResponse response = await (HttpWebResponse)request.GetResponseAsync(); show cannot convert httpwebresponse -> getresponseAsynce.
On universal app not support in HttpUtility
Don't force your typing. Allow it to return what it wants as long as you're able to do what you want with it.
Get rid of the explicit types like this, and you should be fine:
var request = WebRequest.Create(videoInfoUrl);
var response = await request.GetResponseAsync();
The error was occurring because you probably had the (HttpWebResponse) in the wrong location. Where you have it would be trying to convert the Task that GetResponseAsync to the response, which is why you're getting that error.

How to yield until GetResponseStream is finished?

I'm using this code (below) to read a web response, however, I'm getting an incomplete response. If I System.Threading.Thread.Sleep(5000);, right after I run GetResponseStream(); however, I get the complete response.
HttpWebRequest http = (HttpWebRequest)HttpWebRequest.Create(url))
HttpWebResponse response = (HttpWebResponse) http.GetResponse();
using (StreamReader sr = new StreamReader(response.GetResponseStream()))
{
string response = sr.ReadToEnd();
}
How can I yield until it has completed?

C#/XAML HTTPclient/webrequest GET with await never ends

Why won't this HTTPHandler work? Both getPageBody and getPageContent await forever and never get back to me. Nothing else happens after the await (used a break point).
Any help would be strongly appreciated!
PS: Visiting the page in the browser does work - so the problem must be on the C# end.
public class HTTPHandler
{
public static async Task<List<String>> getPageBody(String page)
{
WebRequest request = WebRequest.Create(
"http://www.mywebsite.com/dev/api/" + page);
request.Credentials = CredentialCache.DefaultCredentials;
WebResponse response = await request.GetResponseAsync();
Stream dataStream = response.GetResponseStream();
StreamReader reader = new StreamReader(dataStream);
string responseFromServer = reader.ReadToEnd();
//reader.Close();
//response.Close();
return responseFromServer.Split(';').ToList();
}
public static async Task<List<String>> getPageContents(String page)
{
HttpClient client = new HttpClient();
Task<HttpResponseMessage> resp;
await (resp = client.GetAsync("http://www.mywebsite.com/dev/api/" + page)).ContinueWith(
(getTask) =>
{
getTask.Result.EnsureSuccessStatusCode();
});
//HttpResponseMessage resp = await client.GetAsync("http://mywebsite.com/dev/api/" + page);
Task<String> responseBodyAsText = resp.Result.Content.ReadAsStringAsync();
responseBodyAsText.Wait();
return responseBodyAsText.Result.Split(';').ToList();
}
}
I suspect that further up your call stack, you're using Task.Wait or Task.Result.
If you use Wait or Result on async code, you run the risk of deadlock. I explain this in more detail on my blog and in a recent MSDN article.

C# HttpWebRequest times out after two server 500 errors

After I make two C# HttpWebRequests that throw an exception because of "(500) Internal Server Error 500", the third attempt throws a time out exception. Why doesn't it throw another (500) Internal Server Error exception?
When I restart my application, it throws two 500 errors and then starts timing out again.
This is my code:
GetPages GetPages = new GetPages();
string test = GetPages.GetPage(); /* Exception: (500) Internal Server Error */
GetPages.Dispose();
GetPages GetPages = new GetPages();
string test = GetPages.GetPage(); /* Exception: (500) Internal Server Error */
GetPages.Dispose();
GetPages GetPages = new GetPages();
string test = GetPages.GetPage(); /* Exception: time out, why? */
GetPages.Dispose();
This is GetPages class and GetPage method:
namespace MyNamespace
{
class GetPages
{
public string GetPage()
{
this.httpRequest = (HttpWebRequest)WebRequest.Create("http://myurl");
try
{
StringBuilder postData = new StringBuilder(100);
postData.Append("test=test");
byte[] dataArray = Encoding.UTF8.GetBytes(postData.ToString());
httpRequest.CachePolicy = new HttpRequestCachePolicy(HttpRequestCacheLevel.NoCacheNoStore);
httpRequest.KeepAlive = false;
httpRequest.Proxy = null;
httpRequest.Method = "POST";
httpRequest.Timeout = 10;
httpRequest.ContentType = "application/x-www-form-urlencoded";
httpRequest.ContentLength = dataArray.Length;
using (this.requestStream = httpRequest.GetRequestStream())
{
requestStream.Write(dataArray, 0, dataArray.Length);
requestStream.Flush();
requestStream.Close();
this.webResponse = (HttpWebResponse)httpRequest.GetResponse();
Stream responseStream = webResponse.GetResponseStream();
StreamReader responseReader = new System.IO.StreamReader(responseStream, Encoding.UTF8);
String responseString = responseReader.ReadToEnd();
MessageBox.Show(responseString);
return responseString;
}
}
catch (Exception e)
{
MessageBox.Show(e.Message);
return "FAIL";
}
}
public void Dispose()
{
System.GC.SuppressFinalize(this);
}
}
}
UPDATE
Thanks you all for helping out. I have not been able to solve the issue.
The dispose method is gone now.
I have made HttpWebRequest httpRequest, HttpWebResponse webResponse and Stream requestStream local and am using the following using statements:
using (webResponse = (HttpWebResponse)httpRequest.GetResponse())
{
using (Stream responseStream = webResponse.GetResponseStream())
{
using (StreamReader responseReader = new System.IO.StreamReader(responseStream, Encoding.UTF8))
{
responseString = responseReader.ReadToEnd();
}
}
}
Another update
This is the entire GetPage method now:
public string GetPage()
{
HttpWebRequest httpRequest;
HttpWebResponse webResponse;
Stream requestStream;
try
{
StringBuilder postData = new StringBuilder(100);
postData.Append("test=test");
byte[] dataArray = Encoding.UTF8.GetBytes(postData.ToString());
httpRequest = (HttpWebRequest)WebRequest.Create("http://myurl");
httpRequest.Proxy = null;
httpRequest.Method = "POST";
httpRequest.Timeout = 10;
httpRequest.ContentType = "application/x-www-form-urlencoded";
httpRequest.ContentLength = dataArray.Length;
using (requestStream = httpRequest.GetRequestStream())
{
/* this is never reached when the time out exception starts
so the error seems to be related to too many GetRequestStreams */
requestStream.Write(dataArray, 0, dataArray.Length);
webResponse = (HttpWebResponse)httpRequest.GetResponse();
/* this is never reached when the 500 server error occurs */
String responseString = "";
using (Stream responseStream = webResponse.GetResponseStream())
{
using (StreamReader responseReader = new System.IO.StreamReader(responseStream, Encoding.UTF8))
{
responseString = responseReader.ReadToEnd();
return responseString;
}
}
}
}
catch (Exception e)
{
return "FAIL";
}
return "...";
}
** SOLVED!! **
httpRequest was not getting abort()'ed. In the catch() block I have added httpRequest.abort(), now it works correctly.
I suspect this is the problem:
this.webResponse = (HttpWebResponse)httpRequest.GetResponse();
Stream responseStream = webResponse.GetResponseStream();
StreamReader responseReader = new StreamReader(responseStream, Encoding.UTF8);
String responseString = responseReader.ReadToEnd();
You're not disposing of any of these disposable objects. That means the connection to the web server will be maintained until finalization, so the connection pool (of two connections per server) won't allow any other requests.
I suggest that:
You move the GetResponse call out of the using statement for the request stream
You remove the explicit calls to Flush and Close for the request stream (disposing of it is good enough)
You make webResponse and webRequest local variables instead of instance variables unless you really need them later, in which case you should dispose of them in your Dispose method.
You use using statements for the WebResponse, Stream, and StreamReader. (The last isn't strictly necessary, as disposing of the stream is good enough.)
You make GetPages not implement IDisposable unless you really need it to.
HTTP protocol defines that only two connection can be made at the same time to the same server. Close the responseStream after successful or unsuccessful reading.

Categories