您的位置:首页 > 理论基础 > 计算机网络

HttpWebRequest 与 HttpWebResponse用法 小记

2010-09-29 14:21 465 查看

一、HttpWebRequest、HttpWebResponse

使用这两个类可以使开发出来的应用程序向统一资源标识符(URI)发出请求,并从该资源中获得响应。

.NET Framework给 WebRequest和WebResponse类提供了3个派生类,它们可以通过HTTP、FTP和file://协议与特定的端点通信。

二、使用HttpWebRequest检索Web页面

/// <summary>
/// 使用HttpWebRequest检索Web页面
/// httpwebrequest与httpwebresponse这两个类使用http
/// 协议进行通信。其最知名的用法是,是编写出来的应用程序
/// 可以通过http向其他web页面发出请求,并解析得到的文本,
/// 以便提取数据。这称为屏幕抓取。
/// </summary>
private void GetWebSite()
{
Uri uri = new Uri("http://www.google.com");
WebRequest request = HttpWebRequest.Create(uri);
request.Method = WebRequestMethods.Http.Get;

WebResponse response = request.GetResponse();
StreamReader sr = new StreamReader(response.GetResponseStream());
string temp = sr.ReadToEnd();
response.Close();
sr.Close();

this.panel1.GroupingText = temp;

}


三、使用HttpWebRequest把数据传送给远程Web页面

private void PostDataToPage()
{
Uri uri = new Uri("http://www.amazon.com/Kirbys-Epic-Yarn-Nintendo-Wii/dp/B003ZCH7DI/ref=sr_1_1"); //Uri("http://www.amazon.com/exec/obidos/search-handle-form/102-5194535-6807312");
string data = "s=videogames&ie=UTF8&qid=1285484221&sr=1-1";//"field-keywords=Professional ASP.NET 3.5";

if (uri.Scheme == Uri.UriSchemeHttp)
{
HttpWebRequest request = HttpWebRequest.Create(uri) as HttpWebRequest;
request.Method = WebRequestMethods.Http.Post;
request.ContentLength = data.Length;
request.ContentType = "application/x-www-form-urlencoded";

StreamWriter sw = new StreamWriter(request.GetRequestStream());
sw.Write(data);
sw.Close();

HttpWebResponse response = request.GetResponse() as HttpWebResponse;
StreamReader sr = new StreamReader(response.GetResponseStream());
string temp = sr.ReadToEnd();
sr.Close();
response.Close();

this.panel2.GroupingText = temp;
}
}

//========================================
//异步操作(参考)
public static ManualResetEvent allDone = new ManualResetEvent(false);
const int BUFFER_SIZE = 1024;
const int DefaultTimeout = 2 * 60 * 1000; // 2 minutes timeout

// Abort the request if the timer fires.
private static void TimeoutCallback(object state, bool timedOut)
{
if (timedOut)
{
HttpWebRequest request = state as HttpWebRequest;
if (request != null)
{
request.Abort();
}
}
}

private void PostDataToPageAsyc(){
Uri uri = new Uri("http://www.amazon.com/Kirbys-Epic-Yarn-Nintendo-Wii/dp/B003ZCH7DI/ref=sr_1_1"); //Uri("http://www.amazon.com/exec/obidos/search-handle-form/102-5194535-6807312");
string data = "s=videogames&ie=UTF8&qid=1285484221&sr=1-1";//"field-keywords=Professional ASP.NET 3.5";

if (uri.Scheme == Uri.UriSchemeHttp)
{
try
{
HttpWebRequest request = HttpWebRequest.Create(uri) as HttpWebRequest;
request.Method = WebRequestMethods.Http.Post;
request.ContentLength = data.Length;
request.ContentType = "application/x-www-form-urlencoded";

StreamWriter sw = new StreamWriter(request.GetRequestStream());
sw.Write(data);
sw.Close();

//request.BeginGetResponse(GetRequestAsync, request);
RequestState myRequestState = new RequestState();
myRequestState.request = request;

//start asynchronous request
IAsyncResult result =
request.BeginGetResponse(new AsyncCallback(ResponseCallBack), myRequestState);

// this line implements the timeout, if there is a timeout, the callback fires and the request becomes aborted
ThreadPool.RegisterWaitForSingleObject(result.AsyncWaitHandle, new WaitOrTimerCallback(TimeoutCallback), request, DefaultTimeout, true);

// The response came in the allowed time. The work processing will happen in the
// callback function.
allDone.WaitOne();

// Release the HttpWebResponse resource.
myRequestState.response.Close();

}
catch (WebException e)
{
Response.Write(e.Message);
}

//HttpWebResponse response = request.GetResponse() as HttpWebResponse;
//StreamReader sr = new StreamReader(response.GetResponseStream());
//string temp = sr.ReadToEnd();
//response.Close();
//sr.Close();

//this.panel2.GroupingText = temp;

}
}

private void ResponseCallBack(IAsyncResult asynchronousResult)
{
//state of request is asynchronous
RequestState myRequestState = asynchronousResult.AsyncState as RequestState;
HttpWebRequest request = myRequestState.request;
myRequestState.response = request.EndGetResponse(asynchronousResult) as HttpWebResponse;

//read thr response into a stream object
Stream responseStream = myRequestState.response.GetResponseStream();
myRequestState.streamResponse = responseStream;

//begin the reading of thr contents of the html page and print it to string
IAsyncResult asynchronousInputRead = responseStream.BeginRead(myRequestState.BufferRead, 0, BUFFER_SIZE, new AsyncCallback(ReadHtmlCallBack), myRequestState);
return;
}

private void ReadHtmlCallBack(IAsyncResult asynchronousResult)
{
try
{
//state of request is asynchronous
RequestState myRequestState = asynchronousResult.AsyncState as RequestState;
Stream responseStream = myRequestState.streamResponse;
int read = responseStream.EndRead(asynchronousResult);

if (read > 0)
{
string temp = System.Text.Encoding.ASCII.GetString(myRequestState.BufferRead, 0, BUFFER_SIZE);
myRequestState.requestData.Append(temp);
IAsyncResult myAsynchronousResult =
responseStream.BeginRead(myRequestState.BufferRead, 0, BUFFER_SIZE, new AsyncCallback(ReadHtmlCallBack), myRequestState);
return;
}
else
{
if (myRequestState.requestData.Length > 1)
{
this.panel2.GroupingText += myRequestState.requestData.ToString();
}
}
}
catch (WebException ex)
{
Response.Write(ex.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;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: