首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > Web Service >

ASP 调用Web Service 异常

2012-01-05 
ASP 调用Web Service 错误VB codeDim url,host,method,xmlns,soapAction,soapRequest,xmlDocumenthost

ASP 调用Web Service 错误

VB code
Dim url,host,method,xmlns,soapAction,soapRequest,xmlDocument 
host = "www.caohejing.web"
url = "http://www.caohejing.web/webservice/news/Default.asmx"
method = "POST"
xmlns = "http://www.caohejing.com/NewsService/"
    soapAction = "http://www.caohejing.com/NewsService/NewsAction"

'xmlDocument = CreateXmlDocument()
    xmlDocument="new"

'拼接请求字符串
soapRequest = "<?xml version='1.0' encoding='utf-8'?>"
soapRequest = soapRequest & " <soap12:Envelope xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:soap12='http://www.w3.org/2003/05/soap-envelope'> "
soapRequest = soapRequest & " <soap12:Body>"
soapRequest = soapRequest & " <NewsAction xmlns='"& xmlns &"'>"
soapRequest = soapRequest & " <newsXML>"& xmlDocument &"</newsXML>"
    'soapRequest = soapRequest & " <newsXML>test msg</newsXML>"
soapRequest = soapRequest & " </NewsAction> "
soapRequest = soapRequest & " </soap12:Body>"
soapRequest = soapRequest & " </soap12:Envelope>"
    response.Write(soapRequest)

'创建XMLHttp连接,并传输数据
Set xmlhttp = server.CreateObject("Msxml2.XMLHTTP")
xmlhttp.Open "POST",url,false
xmlhttp.setRequestHeader "Content-Type", "text/xml;charset=utf-8"
xmlhttp.setRequestHeader "HOST", host
xmlhttp.setRequestHeader "Content-Length",LEN(soapRequest)
xmlhttp.setRequestHeader "SOAPAction", soapAction 'WEBSERVICE的命名空间+ "/" + 调用的方法名称
xmlhttp.Send(SoapRequest)


Web Service 调用的例子
VB code
POST /webservice/news/Default.asmx HTTP/1.1Host: www.caohejing.webContent-Type: text/xml; charset=utf-8Content-Length: lengthSOAPAction: "http://www.caohejing.com/NewsService/NewsAction"<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">  <soap:Body>    <NewsAction xmlns="http://www.caohejing.com/NewsService/">      <newsXML>string</newsXML>    </NewsAction>  </soap:Body></soap:Envelope>

执行结果:400 Bad Request


[解决办法]

400?

就是不存在这个东西。。。

应该首先去确定一下是不是路径有问题。。
[解决办法]
完全按照下面的 拼凑字符串,然后用xmlhttp发送能行吗?

看你2段代码 ,前面的是soap12:Envelope
[解决办法]
客气了
首先 你asp代码中
url = "http://www.caohejing.web/webservice/news/Default.asmx"
这个地址从我这是打不开的
如果是有其他测试地址,先把这里改过来 ws完整的测试地址 

你下面ws的post调用例子里 写的是相对地址POST /webservice/news/Default.asmx HTTP/1.1


[解决办法]
哦 
但是你asp代码里的调用地址写的是带域名的
第三行
url = "http://www.caohejing.web/webservice/news/Default.asmx"
倒数第六行


xmlhttp.Open "POST",url,false

你把这个url改成 本地的测试地址应该就行了
[解决办法]
CreateXmlDocument() 这个函数拼凑的xml应该需要编码才能加入 原始soap字串中

具体怎么做你可以 使用c#写个客户端调用你的ws 然后用fiddler等监听下请求

我这截获的http post调用的原始请求是:

POST http://192.168.1.10:8088/webservices/appadrs2010/service.asmx/VerifyAccessIdentity HTTP/1.1
Accept: */*
Referer: http://192.168.1.10:8088/webservices/appadrs2010/service.asmx?op=VerifyAccessIdentity
Accept-Language: zh-CN
User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; Trident/4.0; EmbeddedWB 14.52 from: http://www.bsalsa.com/ EmbeddedWB 14.52; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E; InfoPath.3; Tablet PC 2.0)
Content-Type: application/x-www-form-urlencoded
Accept-Encoding: gzip, deflate
Host: 192.168.1.10:8088
Content-Length: 137
Connection: Keep-Alive
Pragma: no-cache

sUserName=%3C%3Fxml+version%3D%221.0%22+encoding%3D%22utf-8%22%3F%3E&sPassword=%3C%3Fxml+version%3D%221.0%22+encoding%3D%22utf-8%22%3F%3E

热点排行