ASP 调用Web Service 错误

ydf84 2011-03-07 02:58:52
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 调用的例子
POST /webservice/news/Default.asmx HTTP/1.1
Host: www.caohejing.web
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "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
...全文
118 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
ydf84 2011-08-18
  • 打赏
  • 举报
回复
虽然没有正确的答案...还是要把分数给你.谢谢大家
calmzeal 2011-03-08
  • 打赏
  • 举报
回复
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
ydf84 2011-03-07
  • 打赏
  • 举报
回复
如果讲 xmlDocument = CreateXmlDocument() 注释掉,就没有报错了,但是页面没有显示任何返回值,空白
Web Service

/// <summary>
/// 添加一条新闻
/// </summary>
/// <param name="newsXML">一条新闻组成的xml内容</param>
/// <returns>状态</returns>
[WebMethod]
public string NewsAction(String newsXML)
{
return newsXML+" Web";
}
ydf84 2011-03-07
  • 打赏
  • 举报
回复

<%
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()

'拼接请求字符串
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 & " </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)
'这样就利用XMLHTTP成功发送了与SOAP示例所符的SOAP请求.
'检测一下是否成功:
'Response.Write xmlhttp.Status
'Response.Write xmlhttp.StatusText

If xmlhttp.Status = 200 Then
Set xmlDOC =server.CreateObject("MSXML.DOMDocument")
xmlDOC.load(xmlhttp.responseXML)
xmlStr = xmlDOC.xml

Response.write xmlStr
Set xmlDOC=nothing
Else

Response.Write xmlhttp.Status&" "
Response.Write xmlhttp.StatusText

End if
Set xmlhttp = Nothing


'----------------------------
' 创建一个XML文件,包含一条新闻内容
'----------------------------
function CreateXmlDocument()
Dim xdoc
xdoc = "<?xml version='1.0' encoding='utf-8' ?>"
xdoc = xdoc & "<Wohui_DataExtchangedata>"
xdoc = xdoc & " <NEWSTITLE>"
xdoc = xdoc & " 新闻标题"
xdoc = xdoc & " </NEWSTITLE>"

'操作动作(区分大小写): New:新建,Edit:修改,Delete:删除
xdoc = xdoc & " <OPERATIONTYPE>"
xdoc = xdoc & " New/Edit/Delete"
xdoc = xdoc & " </OPERATIONTYPE>"

xdoc = xdoc & " <CHANGEDATE>"
xdoc = xdoc & " 2011/03/04"
xdoc = xdoc & " </CHANGEDATE>"
xdoc = xdoc & " <NEWSBODY>"
xdoc = xdoc & " <DOCUMENT>"
xdoc = xdoc & " <DICID>"
xdoc = xdoc & " 10"
xdoc = xdoc & " </DICID>"
xdoc = xdoc & " <NEWS_TITLE>"
xdoc = xdoc & " news title"
xdoc = xdoc & " </NEWS_TITLE>"
xdoc = xdoc & " <NEWS_FORM>"
xdoc = xdoc & " form"
xdoc = xdoc & " </NEWS_FORM>"
xdoc = xdoc & " <NEWS_TYPEID>"
xdoc = xdoc & " 134"
xdoc = xdoc & " </NEWS_TYPEID>"
xdoc = xdoc & " <NEWS_CLICKNUM>"
xdoc = xdoc & " 100"
xdoc = xdoc & " </NEWS_CLICKNUM>"
xdoc = xdoc & " <NEWS_AUTHOR>"
xdoc = xdoc & " author"
xdoc = xdoc & " </NEWS_AUTHOR>"

'新闻状态. 1:显示,-1:不显示
xdoc = xdoc & " <NEWS_STATUS>"
xdoc = xdoc & " 1"
xdoc = xdoc & " </NEWS_STATUS>"

'新闻置顶. 1:置顶, -1:不置顶
xdoc = xdoc & " <NEWS_ISTOP>"
xdoc = xdoc & " 1"
xdoc = xdoc & " </NEWS_ISTOP>"

'新闻发布5天后是否显示"New"图标. 1:显示,-1:不显示
xdoc = xdoc & " <NEWS_ISNEW>"
xdoc = xdoc & " 1"
xdoc = xdoc & " </NEWS_ISNEW>"
xdoc = xdoc & " <NEWS_STATRTDATE>"
xdoc = xdoc & " 2011/01/01"
xdoc = xdoc & " </NEWS_STATRTDATE>"
xdoc = xdoc & " <NEWS_ENDDATE>"
xdoc = xdoc & " 2011/01/01"
xdoc = xdoc & " </NEWS_ENDDATE>"
xdoc = xdoc & " <NEWS_CONTENT>"
xdoc = xdoc & " <![CDATA["
xdoc = xdoc & " content......"
xdoc = xdoc & " ]]>"
xdoc = xdoc & " </NEWS_CONTENT>"
xdoc = xdoc & " <FILEPUBATTACHLIST>"
xdoc = xdoc & " <PUBATTACH>"
xdoc = xdoc & " <FILENAME>"
xdoc = xdoc & " filename.doc"
xdoc = xdoc & " </FILENAME>"
xdoc = xdoc & " <FILETYPE>"
xdoc = xdoc & " application/msword"
xdoc = xdoc & " </FILETYPE>"
xdoc = xdoc & " <FILESIZE>"
xdoc = xdoc & " 8234"
xdoc = xdoc & " </FILESIZE>"
xdoc = xdoc & " <FILEEXTENSION>"
xdoc = xdoc & " doc"
xdoc = xdoc & " </FILEEXTENSION>"
xdoc = xdoc & " <ISMAIN>"
xdoc = xdoc & " false"
xdoc = xdoc & " </ISMAIN>"
xdoc = xdoc & " <FILECONTENT>"
xdoc = xdoc & " dddddd"
xdoc = xdoc & " </FILECONTENT>"
xdoc = xdoc & " </PUBATTACH>"
xdoc = xdoc & " </FILEPUBATTACHLIST>"
xdoc = xdoc & " </DOCUMENT>"
xdoc = xdoc & " </NEWSBODY>"
xdoc = xdoc & "</Wohui_DataExtchangedata>"
CreateXmlDocument = xdoc
end function
%>


Web Service

以下是 SOAP 1.2 请求和响应示例。所显示的占位符需替换为实际值。

POST /webservice/news/Default.asmx HTTP/1.1
Host: www.caohejing.web
Content-Type: application/soap+xml; charset=utf-8
Content-Length: length

<?xml version="1.0" encoding="utf-8"?>
<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">
<soap12:Body>
<NewsAction xmlns="http://www.caohejing.com/NewsService/">
<newsXML>string</newsXML>
</NewsAction>
</soap12:Body>
</soap12:Envelope>

HTTP/1.1 200 OK
Content-Type: application/soap+xml; charset=utf-8
Content-Length: length

<?xml version="1.0" encoding="utf-8"?>
<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">
<soap12:Body>
<NewsActionResponse xmlns="http://www.caohejing.com/NewsService/">
<NewsActionResult>string</NewsActionResult>
</NewsActionResponse>
</soap12:Body>
</soap12:Envelope>


运行结果: 400 Bad Request
calmzeal 2011-03-07
  • 打赏
  • 举报
回复

但是你asp代码里的调用地址写的是带域名的
第三行
url = "http://www.caohejing.web/webservice/news/Default.asmx"
倒数第六行
xmlhttp.Open "POST",url,false

你把这个url改成 本地的测试地址应该就行了
ydf84 2011-03-07
  • 打赏
  • 举报
回复
地址是我本地的测试地址,没有发布到外网的服务器,所以你还不能访问这个地址
我现在要用soap协议调用接口
calmzeal 2011-03-07
  • 打赏
  • 举报
回复
客气了
首先 你asp代码中
url = "http://www.caohejing.web/webservice/news/Default.asmx"
这个地址从我这是打不开的
如果是有其他测试地址,先把这里改过来 ws完整的测试地址

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

ydf84 2011-03-07
  • 打赏
  • 举报
回复
calmzeal
那我应该怎么调用?
请赐教.谢谢
calmzeal 2011-03-07
  • 打赏
  • 举报
回复
完全按照下面的 拼凑字符串,然后用xmlhttp发送能行吗?

看你2段代码 ,前面的是soap12:Envelope
porschev 2011-03-07
  • 打赏
  • 举报
回复

400?

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

应该首先去确定一下是不是路径有问题。。
ydf84 2011-03-07
  • 打赏
  • 举报
回复
自己先顶上去

12,162

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 Web Services
社区管理员
  • Web Services社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧