使用sproxy.exe访问基于soap的webservice

时间:2023-03-08 22:21:17

使用vc访问基于soap的webservice有多种方法,其中有一种是使用atlsoap,关于这个可以搜索sproxy.exe文章,不在这介绍(主要是我的写作能力太差)。我写这个日记主要是项记录访问webservice时的认证问题,webservice有多个接口,需要登录后才能访问,最简单的办法是cookie机制。
1、webservice代码片段,代码没啥意义,主要是记录如何实现

namespace SmileBus
{
/// <summary>
/// BusService 的摘要说明
/// </summary>
[WebService(Namespace = "http://www.helloworld.com/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消注释以下行。
// [System.Web.Script.Services.ScriptService]
public class BusService : System.Web.Services.WebService
{
[WebMethod(EnableSession = true)]//这个地方需要手动添加支持session
public string HelloWorld(string UserID)
{ if (HttpContext.Current.Session["login"] != null)
{
return HttpContext.Current.Session["login"].ToString() + "ok";
}
else
return "no login";
}
[WebMethod(EnableSession = true)]
public string Login(string strU,string strP)
{
if (strU == "admin" && strP == "admin")
{
HttpContext.Current.Session["login"] = "ok";
return "ok";
}
else
return "登录失败,用户名或密码错误!";
}
}
}

2、使用sproxy.exe生成代理 
sproxy.exe /wsdl http://127.0.0.1/demo.asmx?wsdl
3、C++中使用

 //让程序支持cookie的重点就是这个地方使用CSoapWininetClient,不能用CSoapSocketClientT,CSoapSocketClientT不支持携带cookie
BusService::CBusServiceT<CSoapWininetClient> * m_srv = new BusService::CBusServiceT<CSoapWininetClient>;
CComBSTR str;
m_srv->Login(L"admin", L"admin",&str );
m_srv->HelloWorld(&str);