WinHTTP 错误 URL 无效

发布于 2024-10-07 05:04:15 字数 1224 浏览 8 评论 0原文

如何指定带有路径的 URL,例如:http://stackoverflow.com/questions

以下内容有效,但不适用于上面示例中指定的 URL。

 LPCWSTR useragent = L"Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.12) Gecko/20101026 Firefox/3.6.12 GTB7.1 ( .NET CLR 3.5.30729)";
 DWORD dwSize = 0;
 DWORD dwDownloaded = 0;
 LPSTR pszOutBuffer;
 BOOL  bResults = FALSE;
 HINTERNET  hSession = NULL, hConnect = NULL, hRequest = NULL;

 // Use WinHttpOpen to obtain a session handle.
 hSession = WinHttpOpen(useragent,  
        WINHTTP_ACCESS_TYPE_DEFAULT_PROXY,
        WINHTTP_NO_PROXY_NAME, 
        WINHTTP_NO_PROXY_BYPASS, 0 );

 // Specify an HTTP server.
 if( hSession )
  hConnect = WinHttpConnect( hSession, L"www.stackoverflow.com",
          INTERNET_DEFAULT_HTTPS_PORT, 0 );

 // Create an HTTP request handle.
 if( hConnect )
 hRequest = WinHttpOpenRequest( hConnect, L"GET", NULL, NULL,WINHTTP_NO_REFERER, 
       WINHTTP_DEFAULT_ACCEPT_TYPES, 
      WINHTTP_FLAG_SECURE );

 // Send a request.
  if( hRequest ) bResults = WinHttpSendRequest(hRequest, WINHTTP_NO_ADDITIONAL_HEADERS, 0, WINHTTP_NO_REQUEST_DATA, 0, 0, 0 );

 // End the request.
 if( bResults )
 bResults = WinHttpReceiveResponse( hRequest, NULL );

How do you specify a URL with path for example: http://stackoverflow.com/questions.

The following works but not with the URL specified above in example.

 LPCWSTR useragent = L"Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.12) Gecko/20101026 Firefox/3.6.12 GTB7.1 ( .NET CLR 3.5.30729)";
 DWORD dwSize = 0;
 DWORD dwDownloaded = 0;
 LPSTR pszOutBuffer;
 BOOL  bResults = FALSE;
 HINTERNET  hSession = NULL, hConnect = NULL, hRequest = NULL;

 // Use WinHttpOpen to obtain a session handle.
 hSession = WinHttpOpen(useragent,  
        WINHTTP_ACCESS_TYPE_DEFAULT_PROXY,
        WINHTTP_NO_PROXY_NAME, 
        WINHTTP_NO_PROXY_BYPASS, 0 );

 // Specify an HTTP server.
 if( hSession )
  hConnect = WinHttpConnect( hSession, L"www.stackoverflow.com",
          INTERNET_DEFAULT_HTTPS_PORT, 0 );

 // Create an HTTP request handle.
 if( hConnect )
 hRequest = WinHttpOpenRequest( hConnect, L"GET", NULL, NULL,WINHTTP_NO_REFERER, 
       WINHTTP_DEFAULT_ACCEPT_TYPES, 
      WINHTTP_FLAG_SECURE );

 // Send a request.
  if( hRequest ) bResults = WinHttpSendRequest(hRequest, WINHTTP_NO_ADDITIONAL_HEADERS, 0, WINHTTP_NO_REQUEST_DATA, 0, 0, 0 );

 // End the request.
 if( bResults )
 bResults = WinHttpReceiveResponse( hRequest, NULL );

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一场春暖 2024-10-14 05:04:15

WinHttpConnect 仅采用主机名部分。您应该将路径的其余部分传递给 WinHttpOpenRequest(第三个参数,pwszObjectName):

WinHttpOpenRequest( hConnect, L"GET", L"/questions", NULL,WINHTTP_NO_REFERER, 
       WINHTTP_DEFAULT_ACCEPT_TYPES, 
      WINHTTP_FLAG_SECURE );

WinHttpConnect takes only the hostname part. You should pass the rest of the path to WinHttpOpenRequest (the third parameter, pwszObjectName):

WinHttpOpenRequest( hConnect, L"GET", L"/questions", NULL,WINHTTP_NO_REFERER, 
       WINHTTP_DEFAULT_ACCEPT_TYPES, 
      WINHTTP_FLAG_SECURE );
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文