8,738
社区成员
发帖
与我相关
我的任务
分享
try
{
System.Uri uri = new Uri("http://www.google.com");
// 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 it's 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)
{
outputBlock.Text += "\nException raised!\n";
outputBlock.Text += "Message: ";
outputBlock.Text += e.Message;
outputBlock.Text += "\nStatus: ";
outputBlock.Text += e.Status;
outputBlock.Text += "\n";
}
catch(Exception e)
{
outputBlock.Text += "\nException raised!\n";
outputBlock.Text += "\nMessage: ";
outputBlock.Text += e.Message;
outputBlock.Text += "\n";
}
}
private static void RespCallback(IAsyncResult asynchronousResult)
{
try
{
// State of request is asynchronous.
RequestState myRequestState=(RequestState) asynchronousResult.AsyncState;
HttpWebRequest myHttpWebRequest2=myRequestState.request;
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)
{
// Need to handle the exception
}
}
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();
// do something with the response stream here
}
responseStream.Close();
allDone.Set();
}
}
catch(WebException e)
{
// Need to handle the exception
}
}
public class MyWebClient : WebClient
{
[System.Security.SecuritySafeCritical]
public MyWebClient()
: base()
{
}
}
public class WebClientWithCookies : WebClient
{
private readonly CookieContainer _cookies = new CookieContainer();
public CookieContainer Cookies { get { return _cookies; } }
[SecuritySafeCritical]
public WebClientWithCookies()
: base()
{
}
protected override WebRequest GetWebRequest(Uri address)
{
WebRequest request = base.GetWebRequest(address);
if (request.GetType() == typeof(HttpWebRequest))
((HttpWebRequest)request).CookieContainer = _cookies;
return request;
}
}