1:利用System.Net.WebRequest的Create方法创建WebRequest对象:
System.Net.WebRequestObjWebRequest =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:指明协议的类型:
可以是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.StreamdataStream = ObjWebRequest.GetRequestStream();
7:发送数据:
注:byteArray是一个Byte数组,它的获取可以是:
8:关闭Request流.
dataStream.Close();
这是必须的,知道Close,服务器不会响应输出,还可以选择调用System.IO.Stream.Close()关闭流.
9:利用GetResponse()获取Response对象.
9*:你可以获取返回给客户端的 HTTP 输出状态的字符串,默认是公共状态消息"OK"
//Console.WriteLine(((HttpWebResponse)Objresponse).StatusDescription);
if( ((HttpWebResponse)Objresponse).StatusDescription != "OK"){
//..结束这一切
}
10:获取响应流的句柄:
11:保存数据或者直接读出,例如直接输出到浏览器.
dataStream= ObjResponse.GetResponseStream ();
StreamReaderr = new StreamReader(dataStream);
Response.Write(HttpUtility.HtmlEncode(r.ReadToEnd()));
或者使用StreamWriter对象保存:
此方式可以保存二进制数据.
12:关闭流.
例如发送Soap文件时候,可以创建XmlDocument对象,直接使用Save()即可:
XmlDocumentdoc = new XmlDocument();
doc.Load(System.Web.HttpContext.Current.Server.MapPath(@"~")+@"\" + xmlfile);
HttpWebRequestreq = (HttpWebRequest)WebRequest.Create(@"http://s:81/5do8/webs.asmx");
req.ContentType= "text/xml;charset=\"utf-8\"";
req.Accept= "text/xml";
req.Method= "POST";
Streamstm = req.GetRequestStream();
doc.Save(stm);
stm.Close();
WebResponseresp = req.GetResponse();
stm= resp.GetResponseStream();
StreamReaderr = new StreamReader(stm);
Response.Write(HttpUtility.HtmlEncode(r.ReadToEnd()));
-1:利用ASP发送XMLHTTP对象:
Setxmlhttp = 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
Setxmlhttp = Nothing
获取的时候:
Ifxmlhttp.Status = 200 Then
SetxmlDOC =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
Endif
请求正确则给出完整响应,请求不正确(如账号,密码不对)响应的内容就会信息不完整.
取出响应里的数据,如下:
Ifxmlhttp.Status = 200 Then
SetxmlDOC = server.CreateObject("MSXML.DOMDocument")
xmlDOC.load(xmlhttp.responseXML)
Response.WritexmlDOC.documentElement.selectNodes("//LoginByAccountResult")(0).text
SetxmlDOC = nothing
Else
Response.Writexmlhttp.Status&" "
Response.Writexmlhttp.StatusText
Endif
显示某节点各个属性和数据的FUNCTION:
Functionshowallnode(rootname,myxmlDOC)
ifrootname<>"" then
setnodeobj=myxmlDOC.documentElement.selectSingleNode("//"&rootname&"")'当前结点对像
nodeAttributelen=myxmlDOC.documentElement.selectSingleNode("//"&rootname&"").attributes.length'当前结点属性数
returnstring=returnstring&"<BR>节点名称:"&rootname
ifnodeobj.text<>"" then
returnstring=returnstring&"<BR>节点的文本:("&nodeobj.text&")"
endif
returnstring=returnstring&"<BR>{<BR>"
ifnodeAttributelen<>0 then
returnstring=returnstring&"<BR>属性数有 "&nodeAttributelen&" 个,分别是:"
endif
fori=0 to nodeAttributelen-1
returnstring=returnstring&"<li>"&nodeobj.attributes(i).Name&":"&nodeobj.getAttribute(nodeobj.attributes(i).Name)&" </li>"
next
ifnodeobj.childNodes.Length<>0 then
ifnodeobj.hasChildNodes() and lcase(nodeobj.childNodes.item(0).nodeName)<>"#text" then'是否有子节点
setchildnodeobj=nodeobj.childNodes
childnodelen=nodeobj.childNodes.Length
returnstring=returnstring&"<BR><BR>有"&childnodelen&" 个子节点;<BR>分别是: "
fori=0 to childnodelen-1
returnstring=returnstring&"<li>"&childnodeobj.item(i).nodeName&"</li>"
next
endif
endif
returnstring=returnstring&"<BR>}<BR>"
response.writereturnstring
setnodeobj=nothing
endif
EndFunction
可以这样用:
Ifxmlhttp.Status = 200 Then
SetxmlDOC = server.CreateObject("MSXML.DOMDocument")
xmlDOC.load(xmlhttp.responseXML)
showallnode"LoginByAccountResponse",xmlDOC’调用SHOWALLNODE
SetxmlDOC = nothing
Else
Response.Writexmlhttp.Status&" "
Response.Writexmlhttp.StatusText
Endif
POST:
url= "http://s/"
SoapRequest="username="&username&"&password="&password
Setxmlhttp = 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.Writexmlhttp.Status&” ”
Response.Writexmlhttp.StatusText
Setxmlhttp = Nothing
'POST
Ifxmlhttp.Status = 200 Then
SetxmlDOC = server.CreateObject("MSXML.DOMDocument")
xmlDOC.load(xmlhttp.responseXML)
showallnode"string",xmlDOC'调用SHOWALLNODE
SetxmlDOC = nothing
Else
Response.Writexmlhttp.Status&" "
Response.Writexmlhttp.StatusText
Endif
System.Net.WebRequest
异常类型:
NotSupportedException
ArgumentNullException
SecurityException
ObjWebRequest.Credentials
如果需要按照特别的验证协议,需要强制转换成HttpWebRequest类型:
ObjWebRequest.Method = "POST";
ObjWebRequest.ContentLength
ObjWebRequest.ContentType="application/x-www-form-urlencoded";//Post
ObjWebRequest.ContentType="text/xml";//SOAP
System.IO.Stream
dataStream.Write (byteArray, 0, byteArray.Length);
byte[] byteArray = UTF8Encoding.GetBytes(dataP);
dataStream.Close
WebResponse Objresponse = ObjWebRequest.GetResponse();
//Console.WriteLine
if
//..结束这一切
}
Stream data = Objresponse.GetResponseStream
dataStream
StreamReader
Response.Write(HttpUtility.HtmlEncode(r.ReadToEnd()));
Stream outStream =System.IO.File.Create( "Temp.data" );
byte[]buffer = new byte[1024];
intl;
do{
l= dataStream.Read(buffer,0,buffer.Length);
if(l>0)outStream.Write(buffer,0,l);
}while(l>0);
outStream.Close();
byte
int
do{
l
if(l>0)
}
outStream.Close();
此方式可以保存二进制数据.
Objresponse.Close();
XmlDocument
doc.Load(
HttpWebRequest
req.ContentType
req.Accept
req.Method
Stream
doc.Save(stm);
stm.Close();
WebResponse
stm
StreamReader
Response.Write(HttpUtility.HtmlEncode(r.ReadToEnd()));
Set
xmlhttp.Open
xmlhttp.setRequestHeader
xmlhttp.setRequestHeader
xmlhttp.setRequestHeader
xmlhttp.setRequestHeader
xmlhttp.Send(SoapRequest)
Response.Write
Response.Write
Set
获取的时候:
If
Set
xmlDOC.load(xmlhttp.responseXML)
xmlStr
Set
xmlStr
xmlStr
Response.write
Else
Response.Write
Response.Write
End
请求正确则给出完整响应,请求不正确(如账号,密码不对)响应的内容就会信息不完整.
取出响应里的数据,如下:
If
Set
xmlDOC.load(xmlhttp.responseXML)
Response.Write
Set
Else
Response.Write
Response.Write
End
显示某节点各个属性和数据的FUNCTION:
Function
if
set
nodeAttributelen=myxmlDOC.documentElement.selectSingleNode("//"&rootname&"").attributes.length'当前结点属性数
returnstring=returnstring&"<BR>节点名称:"&rootname
if
returnstring=returnstring&"<BR>节点的文本:("&nodeobj.text&")"
end
returnstring=returnstring&"<BR>{<BR>"
if
returnstring=returnstring&"<BR>属性数有
end
for
returnstring=returnstring&"<li>"&nodeobj.attributes(i).Name&":
next
if
if
set
childnodelen=nodeobj.childNodes.Length
returnstring=returnstring&"<BR><BR>有
for
returnstring=returnstring&"<li>"&childnodeobj.item(i).nodeName&"</li>"
next
end
end
returnstring=returnstring&"<BR>}<BR>"
response.write
set
end
End
可以这样用:
If
Set
xmlDOC.load(xmlhttp.responseXML)
showallnode
Set
Else
Response.Write
Response.Write
End
POST:
url
SoapRequest="username="&username&"&password="&password
Set
xmlhttp.Open
xmlhttp.setRequestHeader
xmlhttp.setRequestHeader
"
xmlhttp.setRequestHeader
xmlhttp.Send(SoapRequest)
Response.Write
Response.Write
Set
'POST
If
Set
xmlDOC.load(xmlhttp.responseXML)
showallnode
Set
Else
Response.Write
Response.Write
End