使用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);