HttpWebRequest httpWebRequest = (HttpWebRequest) WebRequest.Create(uri);
try {
using(HttpWebResponse httpWebResponse = (HttpWebResponse) httpWebRequest.GetResponse())
- using(Stream replyStream = httpWebResponse.GetResponseStream())
- using(StreamReader replyStreamReader = new StreamReader(replyStream))
{
- Console.WriteLine(replyStreamReader.ReadToEnd());
- httpWebResponse.Dispose ();
+ // May httpWebResponse be null? API says nothing (as usual...) API sucks.
+ if (httpWebResponse.StatusCode == HttpStatusCode.OK)
+ {
+ using(Stream replyStream = httpWebResponse.GetResponseStream())
+ using(StreamReader replyStreamReader = new StreamReader(replyStream))
+ {
+ Console.WriteLine(replyStreamReader.ReadToEnd());
+ httpWebResponse.Dispose ();
+ }
+ }
}
+
}
catch(ProtocolViolationException e) {
Console.WriteLine ("Synchronous HttpWebRequest, ProtocolViolationException: ", e);
return true;
} else {
Console.WriteLine ("Cancelling a Task, dunno what are you: {0}", e);
- return false;
+ return true;
}
});
}
if (task.Status == TaskStatus.RanToCompletion)
{
- try {
- using(HttpWebResponse httpWebResponse = (HttpWebResponse) task.Result)
- using(Stream replyStream = httpWebResponse.GetResponseStream())
- using (StreamReader replyStreamReader = new StreamReader (replyStream))
+ using(HttpWebResponse httpWebResponse = (HttpWebResponse) task.Result)
+ {
+ // May httpWebResponse be null? API says nothing (as usual...) API sucks.
+ if (httpWebResponse.StatusCode == HttpStatusCode.OK)
{
- string s = replyStreamReader.ReadToEnd ();
- Console.WriteLine (s);
+ try {
+ using(Stream replyStream = httpWebResponse.GetResponseStream())
+ using (StreamReader replyStreamReader = new StreamReader (replyStream))
+ {
+ string s = replyStreamReader.ReadToEnd ();
+ Console.WriteLine (s);
+ }
+ }
+ catch(ProtocolViolationException e) {
+ Console.WriteLine ("Asynchronous HttpWebRequest, ProtocolViolationException: ", e);
+ }
+ catch(IOException e) {
+ Console.WriteLine ("Asynchronous HttpWebRequest, IOException: ", e);
+ }
}
}
- catch(ProtocolViolationException e) {
- Console.WriteLine ("Asynchronous HttpWebRequest, ProtocolViolationException: ", e);
- }
- catch(NotSupportedException e) {
- Console.WriteLine ("Asynchronous HttpWebRequest, NotSupportedException: ", e);
- }
- catch(WebException e) {
- Console.WriteLine ("Asynchronous HttpWebRequest, WebException: ", e);
- }
- catch(IOException e) {
- Console.WriteLine ("Asynchronous HttpWebRequest, IOException: ", e);
- }
}
}
}
client.Headers.Add("user-agent", "Mozilla/4.0 (compatible; Linux; Mono .NET 4.5)");
try {
+ // HOW MAY I CHECK THE HTTP RESPONSE CODE? THIS SUCKS!!!
+ // From this answer: http://stackoverflow.com/questions/3574659/how-to-get-status-code-from-webclient
+ // When are we receiving WebException? When HTTP status code is not 200? I do not think so.
+ // What if I just want to read data when HTTP OK? I am getting really angry.
+ // C# is a great language but the API sucks IMHO.
+
// Be careful!!! If calling Dispose may throw exception, you could hide
// exceptions being thrown inside the using blocks. In this case this Dispose methods
// do not throw any exception but in other cases it could be different, so never forget it.
return true;
} else {
Console.WriteLine ("Cancelling a Task, dunno what are you: {0}", e);
- return false;
+ return true;
}
});
}
if (task.Status == TaskStatus.RanToCompletion)
{
+ // HOW MAY I CHECK THE HTTP RESPONSE CODE? THIS SUCKS!!!
+ // From this answer: http://stackoverflow.com/questions/3574659/how-to-get-status-code-from-webclient
+ // When are we receiving WebException? When HTTP status code is not 200? I do not think so.
+ // What if I just want to read data when HTTP OK? I am getting really angry.
+ // C# is a great language but the API sucks IMHO.
+
try {
using (Stream replyStream = task.Result)
using (StreamReader replyStreamReader = new StreamReader (replyStream))
private void OpenReadCallback (Object sender, OpenReadCompletedEventArgs eventData)
{
+ // HOW MAY I CHECK THE HTTP RESPONSE CODE? THIS SUCKS!!!
+ // From this answer: http://stackoverflow.com/questions/3574659/how-to-get-status-code-from-webclient
+ // When are we receiving WebException? When HTTP status code is not 200? I do not think so.
+ // What if I just want to read data when HTTP OK? I am getting really angry.
+ // The Mono implementation does not seem to throw WebException... so, the stack overflow answer must be wrong... FUUUUU
+ // C# is a great language but the API sucks IMHO.
+
var taskId = (Guid) eventData.UserState;
if (eventData.Cancelled)