void
GetReqeustStreamCallback(IAsyncResult asynchronousResult)
{
if
(!asynchronousResult.IsCompleted)
return
;
RequestState reqstate =
null
;
try
{
reqstate = (RequestState)asynchronousResult.AsyncState;
HttpWebRequest webRequest = (HttpWebRequest)reqstate.Request;
Stream postStream = webRequest.EndGetRequestStream(asynchronousResult);
string
postData =
""
;
for
(
int
i = 0; i < paramNames.Count; i++)
{
if
(paramNames[i] ==
"POSTDATA"
)
{
postData = paramValues[i];
break
;
}
else
{
if
(i > 0)
{
postData +=
"&"
;
}
postData += paramNames[i] +
"="
+ paramValues[i];
}
}
byte
[] byteArray = Encoding.UTF8.GetBytes(postData);
postStream.Write(byteArray, 0, postData.Length);
postStream.Close();
ThreadPool.QueueUserWorkItem(
new
WaitCallback(target =>
{
try
{
RequestState reqstate2 =
new
RequestState();
reqstate2.Request = webRequest;
reqstate2.Data =
"passed data"
;
reqstate2.AllDone =
new
AutoResetEvent(
false
);
IAsyncResult result = (IAsyncResult)webRequest.BeginGetResponse(
new
AsyncCallback(GetResponseCallback), reqstate2);
bool
waitOneResult =
true
;
if
(!reqstate2.AllDone.WaitOne(DefaultTimeout))
{
waitOneResult =
false
;
if
(webRequest !=
null
)
webRequest.Abort();
}
}
catch
(WebException webExcp)
{
WebExceptionStatus status = webExcp.Status;
if
(status == WebExceptionStatus.ProtocolError)
{
HttpWebResponse httpResponse = (HttpWebResponse)webExcp.Response;
this
.Dispatcher.BeginInvoke(() =>
{
progressBar1.IsLoading =
false
;
MessageBox.Show(
"Unable to reach site. Please try later! "
+ (
int
)httpResponse.StatusCode +
" - "
+ httpResponse.StatusCode +
"."
);
});
}
}
catch
(Exception ex)
{
this
.Dispatcher.BeginInvoke(() =>
{
progressBar1.IsLoading =
false
;
MessageBox.Show(
"BGR1: "
+ ex.Message);
});
}
}
));
}
catch
(WebException webExcp)
{
WebExceptionStatus status = webExcp.Status;
if
(status == WebExceptionStatus.ProtocolError)
{
HttpWebResponse httpResponse = (HttpWebResponse)webExcp.Response;
this
.Dispatcher.BeginInvoke(() =>
{
progressBar1.IsLoading =
false
;
MessageBox.Show(
"Unable to reach site."
+ (
int
)httpResponse.StatusCode +
" - "
+ httpResponse.StatusCode +
"."
);
});
}
}
catch
(Exception ex)
{
this
.Dispatcher.BeginInvoke(() =>
{
progressBar1.IsLoading =
false
;
MessageBox.Show(
"BGR3: "
+ ex.Message);
});
}
}