Tải bản đầy đủ (.pptx) (16 trang)

Một số thao tác với httpwebrequest

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (162.22 KB, 16 trang )

Mộột sộố thao tác vớối HttpWebRequest


Lấốy nộội dung trang web (1)

using System;
using System.IO;
using System.Net;
using System.Text;
class WebFetch
{
static void Main(string[] args)
{
// used to build entire input
StringBuilder sb = new StringBuilder(); 
// used on each read operation
byte[]

buf = new byte[8192];


Lấốy nộội dung trang web (2)

// prepare the web page we will be asking for
HttpWebRequest request = (HttpWebRequest) WebRequest.Create(""); 
// execute the request
HttpWebResponse response = (HttpWebResponse)
// we will read data via the response stream
Stream resStream = response.GetResponseStream(); 

request.GetResponse(); 




Lấốy nộội dung trang web (3)

string tempString = null;
int

count

= 0; 

do {
// fill the buffer with data
count = resStream.Read(buf, 0, buf.Length); 

if (count != 0)

{// translate from bytes to ASCII text

Encoding.ASCII.GetString(buf, 0, count); 
// continue building the string
sb.Append(tempString);
}
} while (count > 0); // any more data to read?

// make sure we read some data

tempString =



Lấốy nộội dung trang web (4)

// print out page source
}
}

Console.WriteLine(sb.ToString());


Giaả lấộp sựộ kiệộn vớối form (1)

action=" method="post">
<input type="hidden" name="cmd" value="_cart">
<input type="hidden" name="business" value="">
<input type="hidden" name="item_name" value="HTML book">
<input type="hidden" name="amount" value="24.99">
<input type="image" src=" />border="0" name="submit" alt="Make payments with PayPal!">
<input type="hidden" name="add" value="1">
</form>


Giaả lấộp sựộ kiệộn vớối form (2)

private void OnPostInfoClick(object sender, System.EventArgs e)
{
string strId = UserId_TextBox.Text;
string strName = Name_TextBox.Text;
ASCIIEncoding encoding=new ASCIIEncoding();
string postData="userid="+strId;

postData += ("&username="+strName);
byte[] data = encoding.GetBytes(postData);


Giaả lấộp sựộ kiệộn vớối form (3)

// Prepare web request...
HttpWebRequest myRequest =
(HttpWebRequest)WebRequest.Create("http://localhost/MyIdentity/Default.aspx");
myRequest.Method = "POST";
myRequest.ContentType="application/x-www-form-urlencoded";
myRequest.ContentLength = data.Length;
Stream newStream=myRequest.GetRequestStream();
// Send the data.
newStream.Write(data,0,data.Length);
newStream.Close();
}


Xác điộn h giá triộ Accept-Encoding header

obRequest.Headers.Add("Accept-Encoding", "gzip");

String strEncoding = obResponse.ContentEncoding;
if (String.Compare("gzip", strEncoding, true) == 0)
{
// Unzip the response stream
}



Simple retrieval of Web data over HTTP.

string lcUrl = " ";
// *** Establish the request
HttpWebRequest loHttp =
(HttpWebRequest) WebRequest.Create(lcUrl);
// *** Set properties
loHttp.Timeout = 10000;

// 10 secs

loHttp.UserAgent = "Code Sample Web Client";
// *** Retrieve request info headers
HttpWebResponse loWebResponse = (HttpWebResponse) loHttp.GetResponse();
Encoding enc = Encoding.GetEncoding(1252); // Windows default Code Page
StreamReader loResponseStream =
new StreamReader(loWebResponse.GetResponseStream(),enc);
string lcHtml = loResponseStream.ReadToEnd();
loWebResponse.Close();
loResponseStream.Close();


POSTing data to the Web Server (1)

string lcUrl = " />HttpWebRequest loHttp =
(HttpWebRequest) WebRequest.Create(lcUrl);
 
// *** Send any POST data
string lcPostData =
"Name= " + HttpUtility.UrlEncode("Rick Strahl") +

"&Company=" + HttpUtility.UrlEncode("West Wind ");
 


POSTing data to the Web Server (2)

loHttp.Method= "POST";
byte [] lbPostBuffer = System.Text.
Encoding.GetEncoding(1252).GetBytes(lcPostData);
loHttp.ContentLength = lbPostBuffer.Length;
 
Stream loPostData = loHttp.GetRequestStream();
loPostData.Write(lbPostBuffer,0,lbPostBuffer.Length);
loPostData.Close();


POSTing data to the Web Server (3)

HttpWebResponse loWebResponse = (HttpWebResponse)
loHttp.GetResponse();
 
Encoding enc = System.Text.Encoding.GetEncoding(1252);
 
StreamReader loResponseStream =
new StreamReader(loWebResponse.GetResponseStream(),enc);
 
string lcHtml = loResponseStream.ReadToEnd();
 
loWebResponse.Close();
loResponseStream.Close();



Authentication

 WebRequest.Credentials = new NetworkCredential("username","password");

 WebRequest.Credentials = CredentialCache.DefaultCredentials


Proxy Server Configuration

// pass Proxy string and bypass local machine
WebProxy loProxy = new WebProxy(":8080",true);
 
// ** ByPassList
string[] cByPass = new string[2];
cByPass[1] = "";
cByPass[2] = http://192.0.0.1
 
loProxy.BypassList = cByPass;
 
// ** Proxy Authentication
loProxy.Credentials = new NetworkCredential("proxyusername","pass");
 
Request.Proxy = loProxy;


Proxy Server Configuration

Request.Proxy = new WebProxy(

":8080",true,cBypass,new NetworkCredential(…) );



×