web services -如何在c++中通过wininternet库连接到webservice

web services - How can I connect to a webservice through winIntet library in c++?

本文关键字:wininternet 连接 webservice services c++ web      更新时间:2023-10-16

我创建了一个webService:

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
// [System.Web.Script.Services.ScriptService]
public class WebService : System.Web.Services.WebService
{
    public WebService()
    {
        //Uncomment the following line if using designed components 
        //InitializeComponent(); 
    }
    [WebMethod]
    public string HelloWorld()
    {
        return "Hello World";
    }
}

服务引用为http://localhost:4653/WebService.asmx。

现在,我需要通过一个cpp项目访问这个web服务。为此,我使用winInet库:

HINTERNET handle = InternetOpen("Upload", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
if (handle)
{
    HINTERNET handleConnect = InternetConnect(handle, "localhost:4653", INTERNET_DEFAULT_HTTP_PORT, NULL, NULL, INTERNET_SERVICE_HTTP, NULL, NULL);
    if (handleConnect)
    {
        if (HINTERNET handleOpenReq = HttpOpenRequest(handleConnect, TEXT("POST"), TEXT("/WebService.asmx/HelloWorld"), NULL, NULL, NULL, INTERNET_FLAG_KEEP_CONNECTION, 0))
        {
            if (HttpSendRequest(handleOpenReq, NULL, 0, NULL, 0))
                cout << "succeed" << endl;
            else
            {
                DWORD p = GetLastError();
            }
        }

然而httpendrequest没有成功,GetLastError返回12029,即试图连接服务器失败。我如何解决这个问题,使它能很好地连接到webService?是不是我发错了什么参数?谢谢你,

不要使用InternetConnect,尝试使用InternetOpenUrl()