1:利用System.Net.WebRequest的Create方法創建WebRequest對象:
System.Net.WebRequest ObjWebRequest=System.Net.WebRequest.Create(urlP);
異常類型:
NotSupportedException:requestUri中指定的請求方案未注冊。
ArgumentNullException:requestUri為空引用(Visual Basic中為Nothing)。
SecurityException調用方沒有連接到請求的URI或請求重定向到的URI上的權限。
2:對請求進行預先身份驗證,如果無明確的要求,此項可以空.
ObjWebRequest.Credentials=CredentialCache.DefaultCredentials;
如果需要按照特別的驗證協議,需要強制轉換成HttpWebRequest類型:
((HttpWebRequest)ObjWebRequest).UserAgent="5DO8";
3:指明協議的類型:
ObjWebRequest.Method="POST";
可以是POST方式和Get方式.
4:發送傳輸的數據長度(如果無顯示傳送,亦可發送),如果設置了此項,發送的數據必須恰好滿足此值.
ObjWebRequest.ContentLength=Stringdata.ToCharArray().Length;
5:設置傳入請求的MIME內容類型:
ObjWebRequest.ContentType="application/x-www-form-urlencoded";//Post
ObjWebRequest.ContentType="text/xml";//SOAP
6:使用GetRequestStream()方法獲取發送的流的句柄:
System.IO.Stream dataStream=ObjWebRequest.GetRequestStream();
7:發送數據:
dataStream.Write(byteArray,0,byteArray.Length);
注:byteArray是一個Byte數組,它的獲取可以是:
byte[] byteArray=UTF8Encoding.GetBytes(dataP);
8:關閉Request流.
dataStream.Close();
這是必須的,知道Close,服務器不會響應輸出,還可以選擇調用System.IO.Stream.Close()關閉流.
9:利用GetResponse()獲取Response對象.
WebResponse Objresponse=ObjWebRequest.GetResponse();
9*:你可以獲取返回給客戶端的HTTP輸出狀態的字符串,默認是公共狀態消息"OK"
//Console.WriteLine(((HttpWebResponse)Objresponse).StatusDescription);
if(((HttpWebResponse)Objresponse).StatusDescription!="OK"){
//..結束這一切
}
10:獲取響應流的句柄:
Stream data=Objresponse.GetResponseStream
11:保存數據或者直接讀出,例如直接輸出到浏覽器.
dataStream=ObjResponse.GetResponseStream();
StreamReader r=new StreamReader(dataStream);
Response.Write(HttpUtility.HtmlEncode(r.ReadToEnd()));
或者使用StreamWriter對象保存:
Stream outStream=System.IO.File.Create("Temp.data");
byte []buffer=new byte[1024];
int l;
do{
l=dataStream.Read(buffer,0,buffer.Length);
if(l>0)outStream.Write(buffer,0,l);
}while(l>0);
outStream.Close();
此方式可以保存二進制數據.
12:關閉流.
Objresponse.Close();
例如發送Soap文件時候,可以創建XmlDocument對象,直接使用Save()即可:
XmlDocument doc=new XmlDocument();
doc.Load(System.Web.HttpContext.Current.Server.MapPath(@"~")+@""+xmlfile);
HttpWebRequest req=(HttpWebRequest)WebRequest.Create(@"http://s:81/5do8/webs.asmx");
req.ContentType="text/xml;charset="utf-8"";
req.Accept="text/xml";
req.Method="POST";
Stream stm=req.GetRequestStream();
doc.Save(stm);
stm.Close();
WebResponse resp=req.GetResponse();
stm=resp.GetResponseStream();
StreamReader r=newStreamReader(stm);
Response.Write(HttpUtility.HtmlEncode(r.ReadToEnd()));
-1:利用ASP發送XMLHTTP對象:
Set xmlhttp=server.CreateObject("Msxml2.XMLHTTP")
xmlhttp.Open"POST",url,false
xmlhttp.setRequestHeader"Content-Type","text/xml;charset=utf-8"
xmlhttp.setRequestHeader"HOST","s"
xmlhttp.setRequestHeader"Content-Length",LEN(SoapRequest)
xmlhttp.setRequestHeader"SOAPAction","http://s"
xmlhttp.Send(SoapRequest)
Response.Writexmlhttp.Status&””
Response.Writexmlhttp.StatusText
Set xmlhttp=Nothing
獲取的時候:
If xmlhttp.Status=200 Then
Set xmlDOC=server.CreateObject("MSXML.DOMDocument")
xmlDOC.load(xmlhttp.responseXML)
xmlStr=xmlDOC.xml
SetxmlDOC=nothing
xmlStr=Replace(xmlStr,"<","<")
xmlStr=Replace(xmlStr,">",">")
Response.writexmlStr
Else
Response.Writexmlhttp.Status&""
Response.Writexmlhttp.StatusText
End if
請求正確則給出完整響應,請求不正確(如賬號,密碼不對)響應的內容就會信息不完整.
取出響應裡的數據,如下:
If xmlhttp.Status=200 Then
Set xmlDOC=server.CreateObject("MSXML.DOMDocument")
xmlDOC.load(xmlhttp.responseXML)
Response.WritexmlDOC.documentElement.selectNodes("//LoginByAccountResult")(0).text
Set xmlDOC=nothing
Else
Response.Writexmlhttp.Status&""
Response.Writexmlhttp.StatusText
End if
顯示某節點各個屬性和數據的FUNCTION:
Function showallnode(rootname,myxmlDOC)
if rootname<>"" then
set nodeobj=myxmlDOC.documentElement.selectSingleNode("//"&rootname&"")'當前結點對像
nodeAttributelen=myxmlDOC.documentElement.selectSingleNode("//"&rootname&"").attributes.length'當前結點屬性數
returnstring=returnstring&"<BR>節點名稱:"&rootname
if nodeobj.text<>"" then
returnstring=returnstring&"<BR>節點的文本:("&nodeobj.text&")"
end if
returnstring=returnstring&"<BR>{<BR>"
if nodeAttributelen<>0 then
returnstring=returnstring&"<BR>屬性數有 "&nodeAttributelen&" 個,分別是:"
end if
for i=0 to nodeAttributelen-1
returnstring=returnstring&"<li>"&nodeobj.attributes(i).Name&": "&nodeobj.getAttribute(nodeobj.attributes(i).Name)&" </li>"
next
if nodeobj.childNodes.Length<>0 then
if nodeobj.hasChildNodes() and lcase(nodeobj.childNodes.item(0).nodeName)<>"#text" then'是否有子節點
set childnodeobj=nodeobj.childNodes
childnodelen=nodeobj.childNodes.Length
returnstring=returnstring&"<BR><BR>有 "&childnodelen&" 個子節點;<BR>分別是: "
for i=0 to childnodelen-1
returnstring=returnstring&"<li>"&childnodeobj.item(i).nodeName&"</li>"
next
end if
end if
returnstring=returnstring&"<BR>}<BR>"
response.write returnstring
set nodeobj=nothing
end if
End Function
可以這樣用:
If xmlhttp.Status = 200 Then
Set xmlDOC = server.CreateObject("MSXML.DOMDocument")
xmlDOC.load(xmlhttp.responseXML)
showallnode "LoginByAccountResponse",xmlDOC’調用SHOWALLNODE
Set xmlDOC = nothing
Else
Response.Write xmlhttp.Status&" "
Response.Write xmlhttp.StatusText
End if
POST:
url = "http://s/" SoapRequest="username="&username&"&password="&password Set xmlhttp = server.CreateObject("Msxml2.XMLHTTP") xmlhttp.Open "POST",url,false xmlhttp.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"’注意 xmlhttp.setRequestHeader "HOST"," " xmlhttp.setRequestHeader "Content-Length",LEN(SoapRequest) xmlhttp.Send(SoapRequest) Response.Write xmlhttp.Status&” ” Response.Write xmlhttp.StatusText Set xmlhttp = Nothing 'POST If xmlhttp.Status = 200 Then Set xmlDOC = server.CreateObject("MSXML.DOMDocument") xmlDOC.load(xmlhttp.responseXML) showallnode "string",xmlDOC'調用SHOWALLNODE Set xmlDOC = nothing Else Response.Write xmlhttp.Status&" " Response.Write xmlhttp.StatusText End if