
Untitled
By: a guest on
Aug 9th, 2012 | syntax:
None | size: 2.82 KB | hits: 7 | expires: Never
public void DoPartnerPost()
{
AsyncWebHelper.PostAsync(postUrl, parms, callbackState =>
{
// Success! Now make partner post
if (callbackState.Exception == null)
{
MoaHelper.MoaResponse resp = AsyncWebHelper.DeSerializeToJson<MoaHelper.MoaResponse>(callbackState.ResponseStream);
//string hiresResult = AsyncWebHelper.GetResponseText(callbackState.ResponseStream);
DoPartnerPost(partnerPostUrl, lowResUrl, resp.Url, actionList, postData);
}
});
}
public static void GetAsync(string url, Action<HttpWebRequestCallbackState> responseCallback,
object state = null, string contentType = "application/x-www-form-urlencoded")
{
var httpWebRequest = CreateHttpWebRequest(url, "GET", contentType);
HttpWebRequestAsyncState asyncState = new HttpWebRequestAsyncState()
{
HttpWebRequest = httpWebRequest,
ResponseCallback = responseCallback,
State = state,
StartTime = DateTime.Now
};
IAsyncResult result = httpWebRequest.BeginGetResponse(BeginGetResponseCallback, asyncState);
// Add Timeout mechanism
ThreadPool.RegisterWaitForSingleObject(result.AsyncWaitHandle, new WaitOrTimerCallback(TimeoutCallback), asyncState, AsyncTimeout, true);
}
static void BeginGetResponseCallback(IAsyncResult asyncResult)
{
WebResponse webResponse = null;
Stream responseStream = null;
HttpWebRequestAsyncState asyncState = null;
try
{
asyncState = (HttpWebRequestAsyncState)asyncResult.AsyncState;
// end this operation
webResponse = asyncState.HttpWebRequest.EndGetResponse(asyncResult);
responseStream = webResponse.GetResponseStream();
var webRequestCallbackState = new HttpWebRequestCallbackState(responseStream, asyncState.State);
asyncState.ResponseCallback(webRequestCallbackState);
// cleanup
responseStream.Close();
responseStream = null;
webResponse.Close();
webResponse = null;
}
catch (Exception ex)
{
if (asyncState != null)
asyncState.ResponseCallback(new HttpWebRequestCallbackState(ex));
else
throw;
}
finally
{
if (responseStream != null)
responseStream.Close();
if (webResponse != null)
webResponse.Close();
}
}