R
Rich P123
Guest
In the following sample program (running in winform vs2012) I am retrieving data from an httpclient and using IAsynResult - asyncronous operation. I need to loop through a list of parameters that I pass to the Demo procedure (below) for creating the url. Some parameters are valid and some parameters are invalid. The invalid params cause an exception, and the program freezes. The error message is
The remote server returned an error: (404) Not Found.
The error occurs at the HttpWebResponse line. When the code execution is in the Demo Procedure -- when the code reaches allDone.WaitOne(); the code goes to the procedure
private static void RespCallback(IAsyncResult asynchronousResult)
The line where the code bombs out is
myRequestState.response = (HttpWebResponse)myHttpWebRequest2.EndGetResponse(asynchronousResult);
I want to know if there is a way I can continue program execution -- prevent the program from freezing on invalid parameters. Maybe I should use different coding altogether? How can I check for this exception and bypass the call to HttpWebResponse if there is evidence of an exception?
using System;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Windows;
using System.Net;
using System.IO;
using System.Threading;
namespace mySampleProject
{
public partial class Form6 : Form
{
private void button1_Click(object sender, EventArgs e)
{
StartDemo();
}
public static ManualResetEvent allDone = new ManualResetEvent(false);
const int BUFFER_SIZE = 1024;
public static string strContent;
public void StartDemo()
{
string myStringContent;
DateTime dStart = new DateTime(2014, 1, 1);
DateTime d1;
EF1_DBContext db1 = new EF1_DBContext();
string s1, s2, s3, s4;
int daysCount = 10;
d1 = dStart.AddDays(daysCount);
string format = "yyyy-MM-dd";
var query = from b in db1.MasterParameterCodes
select b;
foreach (var item in query.ToList())
{
strContent = "";
myStringContent = "";
s1 = "ca";
s2 = dStart.ToString(format);
s3 = d1.ToString(format);
s4 = item.ParameterCode.ToString();
try
{
Demo(s1, s2, s3, s4);
myStringContent = strContent;
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
}
public static void Demo(string s1, string s2, string s3, string s4)
{
try
{
string s_url = "http://" + s1 + "&startDT=" + s2 + "&endDT=" + s3 + "¶meterCd=" + s4;
System.Uri uri = new Uri(s_url);
// Create a HttpWebrequest object to the desired URL.
HttpWebRequest myHttpWebRequest1 = (HttpWebRequest)WebRequest.Create(uri);
// Create an instance of the RequestState and assign the previous myHttpWebRequest1
// object to its request field.
RequestState myRequestState = new RequestState();
myRequestState.request = myHttpWebRequest1;
// Start the asynchronous request.
IAsyncResult result = (IAsyncResult)myHttpWebRequest1.BeginGetResponse(new AsyncCallback(RespCallback), myRequestState);
allDone.WaitOne();
// Release the HttpWebResponse resource.
myRequestState.response.Close();
}
catch (WebException e)
{
Console.WriteLine("Problem at Demo");
Console.WriteLine(e.Message);
}
}
private static void RespCallback(IAsyncResult asynchronousResult)
{
try
{
// State of request is asynchronous.
RequestState myRequestState = (RequestState)asynchronousResult.AsyncState;
HttpWebRequest myHttpWebRequest2 = myRequestState.request;
//--code execution bombs out at this line here
myRequestState.response = (HttpWebResponse)myHttpWebRequest2.EndGetResponse(asynchronousResult);
// Read the response into a Stream object.
Stream responseStream = myRequestState.response.GetResponseStream();
myRequestState.streamResponse = responseStream;
// Begin the Reading of the contents of the HTML page and print it to the console.
IAsyncResult asynchronousInputRead = responseStream.BeginRead(myRequestState.BufferRead, 0, BUFFER_SIZE, new AsyncCallback(ReadCallBack), myRequestState);
}
catch (WebException e)
{
Console.WriteLine("Problem at RespCallback");
Console.WriteLine(e.Message);
}
}
private static void ReadCallBack(IAsyncResult asyncResult)
{
try
{
RequestState myRequestState = (RequestState)asyncResult.AsyncState;
Stream responseStream = myRequestState.streamResponse;
int read = responseStream.EndRead(asyncResult);
// Read the HTML page and then do something with it
if (read > 0)
{
myRequestState.requestData.Append(Encoding.UTF8.GetString(myRequestState.BufferRead, 0, read));
IAsyncResult asynchronousResult = responseStream.BeginRead(myRequestState.BufferRead, 0, BUFFER_SIZE, new AsyncCallback(ReadCallBack), myRequestState);
}
else
{
if (myRequestState.requestData.Length > 1)
{
//string stringContent;
//stringContent = myRequestState.requestData.ToString();
strContent = myRequestState.requestData.ToString();
// do something with the response stream here
//Console.WriteLine(stringContent);
}
responseStream.Close();
allDone.Set();
}
}
catch (WebException e)
{
Console.WriteLine("problem at ReadCallBack");
Console.WriteLine(e.Message);
}
}
}
public class RequestState
{
// This class stores the State of the request.
const int BUFFER_SIZE = 1024;
public StringBuilder requestData;
public byte[] BufferRead;
public HttpWebRequest request;
public HttpWebResponse response;
public Stream streamResponse;
public RequestState()
{
BufferRead = new byte[BUFFER_SIZE];
requestData = new StringBuilder("");
request = null;
streamResponse = null;
}
}
}
Rich P
Continue reading...
The remote server returned an error: (404) Not Found.
The error occurs at the HttpWebResponse line. When the code execution is in the Demo Procedure -- when the code reaches allDone.WaitOne(); the code goes to the procedure
private static void RespCallback(IAsyncResult asynchronousResult)
The line where the code bombs out is
myRequestState.response = (HttpWebResponse)myHttpWebRequest2.EndGetResponse(asynchronousResult);
I want to know if there is a way I can continue program execution -- prevent the program from freezing on invalid parameters. Maybe I should use different coding altogether? How can I check for this exception and bypass the call to HttpWebResponse if there is evidence of an exception?
using System;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Windows;
using System.Net;
using System.IO;
using System.Threading;
namespace mySampleProject
{
public partial class Form6 : Form
{
private void button1_Click(object sender, EventArgs e)
{
StartDemo();
}
public static ManualResetEvent allDone = new ManualResetEvent(false);
const int BUFFER_SIZE = 1024;
public static string strContent;
public void StartDemo()
{
string myStringContent;
DateTime dStart = new DateTime(2014, 1, 1);
DateTime d1;
EF1_DBContext db1 = new EF1_DBContext();
string s1, s2, s3, s4;
int daysCount = 10;
d1 = dStart.AddDays(daysCount);
string format = "yyyy-MM-dd";
var query = from b in db1.MasterParameterCodes
select b;
foreach (var item in query.ToList())
{
strContent = "";
myStringContent = "";
s1 = "ca";
s2 = dStart.ToString(format);
s3 = d1.ToString(format);
s4 = item.ParameterCode.ToString();
try
{
Demo(s1, s2, s3, s4);
myStringContent = strContent;
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
}
public static void Demo(string s1, string s2, string s3, string s4)
{
try
{
string s_url = "http://" + s1 + "&startDT=" + s2 + "&endDT=" + s3 + "¶meterCd=" + s4;
System.Uri uri = new Uri(s_url);
// Create a HttpWebrequest object to the desired URL.
HttpWebRequest myHttpWebRequest1 = (HttpWebRequest)WebRequest.Create(uri);
// Create an instance of the RequestState and assign the previous myHttpWebRequest1
// object to its request field.
RequestState myRequestState = new RequestState();
myRequestState.request = myHttpWebRequest1;
// Start the asynchronous request.
IAsyncResult result = (IAsyncResult)myHttpWebRequest1.BeginGetResponse(new AsyncCallback(RespCallback), myRequestState);
allDone.WaitOne();
// Release the HttpWebResponse resource.
myRequestState.response.Close();
}
catch (WebException e)
{
Console.WriteLine("Problem at Demo");
Console.WriteLine(e.Message);
}
}
private static void RespCallback(IAsyncResult asynchronousResult)
{
try
{
// State of request is asynchronous.
RequestState myRequestState = (RequestState)asynchronousResult.AsyncState;
HttpWebRequest myHttpWebRequest2 = myRequestState.request;
//--code execution bombs out at this line here
myRequestState.response = (HttpWebResponse)myHttpWebRequest2.EndGetResponse(asynchronousResult);
// Read the response into a Stream object.
Stream responseStream = myRequestState.response.GetResponseStream();
myRequestState.streamResponse = responseStream;
// Begin the Reading of the contents of the HTML page and print it to the console.
IAsyncResult asynchronousInputRead = responseStream.BeginRead(myRequestState.BufferRead, 0, BUFFER_SIZE, new AsyncCallback(ReadCallBack), myRequestState);
}
catch (WebException e)
{
Console.WriteLine("Problem at RespCallback");
Console.WriteLine(e.Message);
}
}
private static void ReadCallBack(IAsyncResult asyncResult)
{
try
{
RequestState myRequestState = (RequestState)asyncResult.AsyncState;
Stream responseStream = myRequestState.streamResponse;
int read = responseStream.EndRead(asyncResult);
// Read the HTML page and then do something with it
if (read > 0)
{
myRequestState.requestData.Append(Encoding.UTF8.GetString(myRequestState.BufferRead, 0, read));
IAsyncResult asynchronousResult = responseStream.BeginRead(myRequestState.BufferRead, 0, BUFFER_SIZE, new AsyncCallback(ReadCallBack), myRequestState);
}
else
{
if (myRequestState.requestData.Length > 1)
{
//string stringContent;
//stringContent = myRequestState.requestData.ToString();
strContent = myRequestState.requestData.ToString();
// do something with the response stream here
//Console.WriteLine(stringContent);
}
responseStream.Close();
allDone.Set();
}
}
catch (WebException e)
{
Console.WriteLine("problem at ReadCallBack");
Console.WriteLine(e.Message);
}
}
}
public class RequestState
{
// This class stores the State of the request.
const int BUFFER_SIZE = 1024;
public StringBuilder requestData;
public byte[] BufferRead;
public HttpWebRequest request;
public HttpWebResponse response;
public Stream streamResponse;
public RequestState()
{
BufferRead = new byte[BUFFER_SIZE];
requestData = new StringBuilder("");
request = null;
streamResponse = null;
}
}
}
Rich P
Continue reading...