Fengzhimei@Dot.Net
Designing My Colorful Dream
1/// <summary>
2/// WriteToURL is a method that writes the contents of
3/// a specified URL to the web
4/// </summary>
5/// <param name="request"></param>
6/// <param name="data"></param>
7private void WriteToURL (WebRequest request, string data) 
8{
9 byte [] bytes = null;
10 // Get the data that is being posted (or sent) to the server
11 bytes = System.Text.Encoding.ASCII.GetBytes (data);
12 request.ContentLength = bytes.Length;
13 // 1. Get an output stream from the request object
14 Stream outputStream = request.GetRequestStream ();
15
16 // 2. Post the data out to the stream
17 outputStream.Write (bytes, 0, bytes.Length);
18
19 // 3. Close the output stream and send the data out to the web server
20 outputStream.Close ();
21} // end WriteToURL method
22
23/// <summary>
24/// RetrieveFromURL is a method that retrieves the contents of
25/// a specified URL in response to a request
26/// </summary>
27/// <param name="request"></param>
28/// <returns></returns>
29private string RetrieveFromURL (WebRequest request) 
30{
31 Encoding encoding = Encoding.GetEncoding ("gb2312");
32
33 // 1. Get the Web Response Object from the request
34 WebResponse wrResponse = request.GetResponse ();
35
36 // 2. Get the Stream Object from the response
37 Stream responseStream = wrResponse.GetResponseStream ();
38
39 // 3. Create a stream reader and associate it with the stream object
40 StreamReader reader = new StreamReader (responseStream,encoding);
41
42 // 4. read the entire stream 
43 return reader.ReadToEnd ();
44}// end RetrieveFromURL method
45
46/// <summary>
47/// CreateWebRequest is a method to create and return WebRequest object
48/// </summary>
49/// <param name="strURL"></param>
50/// <param name="strProxy"></param>
51/// <param name="strAccount"></param>
52/// <param name="strPwd"></param>
53/// <param name="strDomain"></param>
54/// <returns></returns>
55private WebRequest CreateWebRequest (string strURL, string strProxy, string strAccount, string strPwd, string strDomain)
56{
57 // Create the Network Credential Object 
58 NetworkCredential cr = new NetworkCredential (strAccount,strPwd,strDomain);
59
60 // Create the Web Proxy Object 
61 WebProxy webProxy = new WebProxy (strProxy,true,null,cr);
62
63 // Create the Web Request Object 
64 WebRequest wrRequest = WebRequest.Create (strURL);
65
66 // Specify the Web Request Object Proxy
67 wrRequest.Proxy = webProxy ;
68
69 return wrRequest;
70}// end CreateWebRequest method
71
72/// <summary>
73///  PostToURL is a method that forces a POST
74///  of data to a specified URL
75///  
76///  A HTTP POST is a combination of a write to the Web Server 
77///  and an immediate read from the Web server
78/// </summary>
79/// <param name="strURL"></param>
80/// <param name="strProxy"></param>
81/// <param name="strAccount"></param>
82/// <param name="strPwd"></param>
83/// <param name="strDomain"></param>
84/// <returns></returns>
85private string PostToURL (string strURL, string strProxy, string strAccount, string strPwd, string strDomain) 
86{
87 // Create the Web Request Object 
88 WebRequest wrRequest = CreateWebRequest (strURL,strProxy,strAccount,strPwd,strDomain);
89
90 // Specify that you want to POST data
91 wrRequest.Method = "POST";
92 wrRequest.ContentType = "application/x-www-form-urlencoded";
93
94 if (strURL != null) 
95 {
96  // write out the data to the web server
97  WriteToURL (wrRequest, strURL);
98 }
99 else 
100 {
101  wrRequest.ContentLength = 0;
102 }
103
104 // read the response from the Web Server
105 string htmlContent = RetrieveFromURL (wrRequest);
106
107 return htmlContent;
108} // end PostToURL method
109
110/// <summary>
111/// GetFromURL is a method that retrieves the contents of
112/// a specified URL in response to a request
113/// </summary>
114/// <param name="strURL"></param>
115/// <param name="strProxy"></param>
116/// <param name="strAccount"></param>
117/// <param name="strPwd"></param>
118/// <param name="strDomain"></param>
119/// <returns></returns>
120private string GetFromURL (string strURL, string strProxy, string strAccount, string strPwd, string strDomain) 
121{
122 // Create the Web Request Object 
123 WebRequest wrRequest = CreateWebRequest (strURL,strProxy,strAccount,strPwd,strDomain);
124
125 // read the response from the Web Server
126 string htmlContent = RetrieveFromURL (wrRequest);
127
128 return htmlContent;
129} // end GetFromURL 
posted on 2004-04-13 01:42  fengzhimei  阅读(703)  评论(0编辑  收藏  举报