: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 = new StreamReader(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.Write xmlhttp.Status&” ”
Response.Write xmlhttp.StatusText
Set xmlhttp = Nothing
获取的时候:
If xmlhttp.Status = 200 Then
Set xmlDOC =server.CreateObject(“MSXML.DOMDocument”)
xmlDOC.load(xmlhttp.responseXML)
xmlStr = xmlDOC.xml
Set xmlDOC=nothing
xmlStr = Replace(xmlStr,”<“,”<“)
xmlStr = Replace(xmlStr,”>”,”>”)
Response.write xmlStr
Else
Response.Write xmlhttp.Status&” ”
Response.Write xmlhttp.StatusText
End if
请求正确则给出完整响应,请求不正确(如账号,密码不对)响应的内容就会信息不完整.
取出响应里的数据,如下:
If xmlhttp.Status = 200 Then
Set xmlDOC = server.CreateObject(“MSXML.DOMDocument”)
xmlDOC.load(xmlhttp.responseXML)
Response.Write xmlDOC.documentElement.selectNodes(“//LoginByAccountResult”)(0).text
Set xmlDOC = nothing
Else
Response.Write xmlhttp.Status&” ”
Response.Write xmlhttp.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