VC++6.0访问webservice
来源:互联网 发布:java求100之内的素数 编辑:程序博客网 时间:2024/04/30 03:55
VC++6.0访问webservice
第一步:安装SoapToolkit30
点击这里 下载地址 http://download.csdn.net/detail/aasmfox/9578708
第二步:安装测试用的webservice
点这里 下载地址:http://download.csdn.net/detail/aasmfox/9578707
第三步:编写测试代码
VC++代码
#import "msxml4.dll" #import "C:\Program Files\Common Files\MSSoap\Binaries\mssoap30.dll" \exclude("IStream", "IErrorInfo", "ISequentialStream", "_LARGE_INTEGER", \"_ULARGE_INTEGER", "tagSTATSTG", "_FILETIME")#define _UNICODEusing namespace MSXML2;using namespace MSSOAPLib30;
CString _wsURL ,_wsNameSapce , _wsMethodName ; _wsURL =_T("http://127.0.0.1/helloworld/service.asmx?wsdl");_wsNameSapce =_T("http://127.0.0.1/helloworld/");_wsMethodName =_T("HelloWorld");try{HRESULT hr = CoInitialize(NULL);//初始化com环境if(FAILED(hr)){//出错了}ISoapSerializerPtr Serializer;ISoapReaderPtr Reader;ISoapConnectorPtr Connector;CString strName ; //连接到WebServicehr = Connector.CreateInstance(__uuidof(HttpConnector30));if(FAILED(hr)){//创建com对象出错,一般是因为没有安装com}Connector->Property["EndPointURL"] = _bstr_t(_wsURL); Connector->Connect();Connector->Property["SoapAction"] =_bstr_t(_T("http://127.0.0.1/helloworld/HelloWorld"));//开始创建webservice的请求Soap包Connector->BeginMessage();hr = Serializer.CreateInstance(__uuidof(SoapSerializer30));if(FAILED(hr)){//创建com对象出错,一般是因为没有安装com}Serializer->Init(_variant_t((IUnknown*)Connector->InputStream));Serializer->StartEnvelope("SOAP", "http://schemas.xmlsoap.org/soap/envelope/", "");Serializer->SoapAttribute("xsi", "", "http://www.w3.org/2001/XMLSchema-instance", "xmlns");Serializer->SoapAttribute("xsd", "", "http://www.w3.org/2001/XMLSchema", "xmlns");Serializer->StartBody("");Serializer->StartElement(_bstr_t(_wsMethodName), _bstr_t(_wsNameSapce), "", ""); Serializer->EndElement();Serializer->EndBody();Serializer->EndEnvelope(); Connector->EndMessage();//解析返回的soap包hr = Reader.CreateInstance(__uuidof(SoapReader30));if(FAILED(hr)){//创建com对象出错,一般是因为没有安装com}Reader->Load(_variant_t((IUnknown*)Connector->OutputStream), "");CString strResult((const char*)Reader->RpcResult->text);MessageBox(strResult,_T("ok"),MB_OK);strResult = (LPCTSTR)_bstr_t(Reader->RpcResult->xml ); MessageBox(strResult,_T("ok"),MB_OK);return ;}catch(_com_error &e){ //got a exception}
webservice 代码
using System;using System.Web;using System.Web.Services;using System.Web.Services.Protocols;[WebService(Namespace = "http://127.0.0.1/helloworld/")][WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]public class Service : System.Web.Services.WebService{ public Service () { //Uncomment the following line if using designed components //InitializeComponent(); } [WebMethod] public string HelloWorld() { return "Hello World"; } }
JavaScript 脚本测试代码
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml" ><head> <title>Untitled Page</title></head><body><script type="text/javascript"> function TestSoap() { // debugger; var indata = document.getElementById("TextArea1").value; var data; data = '<?xml version="1.0" encoding="utf-8"?>'; data = data + '<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/">'; data = data + '<soap:Body>'; data = data + '<Add xmlns="http://127.0.0.1/helloworld">'; data = data + '</Add>'; data = data + '</soap:Body>'; data = data + '</soap:Envelope>'; var xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); var URL = "http://127.0.0.1/helloworld/service.asmx?wsdl"; xmlhttp.Open("POST", URL, false); xmlhttp.SetRequestHeader("Content-Type", "text/xml; charset=utf-8"); xmlhttp.SetRequestHeader("SOAPAction", "http://127.0.0.1/helloworld/HelloWorld"); xmlhttp.Send(data); document.getElementById("message").innerHTML = xmlhttp.responseText; } </script> <textarea id="TextArea1" rows="2" style="width: 366px"></textarea><br /><input type="button" onclick="TestSoap();" value= "click"/> <div id="message"></div></body></html>
测试结果图
总结:
代码都是从网络上复制粘贴得到的,调试这么个小东西,花了不少时间 ,原因在于初学都心浮急躁,不明原理而急于求成。
注意事项:
总是返回<faultcode>soap:Client</faultcode>错误,
1.要检查接口函数大小写,要与webservice中的定义一致,
如http://127.0.0.1/helloworld/HelloWorld 和如http://127.0.0.1/helloworld/helloworld 是不一样的。*2. 名字空间要注意最后的返写杆'/'
_wsNameSapce =_T("http://127.0.0.1/helloworld/");
勿在浮沙筑高台
0 0
- VC++6.0访问webservice
- VC++6.0 用gSoap客户端访问WebService
- vc++ 访问php webService
- 使用 VC++访问webservice
- VC 通过Soap访问WebService
- VC 通过Soap访问WebService
- 访问webService
- 使用VC++ 6.0开发WebService(gsoap)
- 使用VC++ 6.0开发WebService(gsoap)
- Webservice -- HttpCilent 访问Webservice 请求
- vc++调用webservice
- VC++.NET调用WebService
- VC调用WebService例子
- VC+gSoap实现WebService
- vc 调 c# webservice
- vc++调用webservice
- VC调用Webservice
- vc++调用webservice
- 推荐一些常见android开源库
- MyEclipse安装后需要进行的配置
- apache虚拟主机的配置
- Python新手学习基础之数据类型——字符串的切片截取
- bzoj 3531(动态加点线段树,树链剖分)
- VC++6.0访问webservice
- 【记录】常用ARM汇编指令
- 扫描二维码登录原理
- ZYKeyboardUtil 让键盘遮盖事件处理变得得心应手
- hadoop,HDFS常用Shell操作
- Android Studio设置背景颜色为黑色后,复选框颜色依旧是黑色,导致在未选定情况下看不到复选框
- LeetCode Reverse Integer(C)
- 【Leetcod 动态规划】 子数组最大和一类的问题
- 常用vi编辑器命令行