誰知道這C++ 寫HTTP請求怎么寫
uj5u.com熱心網友回復:
使用libcurl庫中的介面實作, https://www.cnblogs.com/heluan/p/10177475.htmluj5u.com熱心網友回復:
GET /index.htm HTTP/1.1后面就是一些頭資訊。Accept encoding之類
uj5u.com熱心網友回復:
使用libcurl庫不會用啊,貼一段代碼 這種訪問服務器 總是回傳 能不能誰幫忙指點下,急!CInternetSession m_cis;//用來創建和初始化因特網會話yyf
CHttpConnection *m_pHttp;//用于HTTP服務
CString m_strIP,m_appliURL,m_strIniPath;
INTERNET_PORT m_strport = 80;
m_strIP = "127.0.0.1" ;
m_appliURL = "https://idserver.xxxkj.com"; //服務器地址
if ( m_appliURL.IsEmpty())
{
//CHXcertinterfaceCtrl::m_Geterror = "連接地址為空";
return 2;
}
//DWORD dwType = AFX_INET_SERVICE_HTTPS; // 互聯網服務器型別
//AfxParseURL( m_appliURL, dwType,m_strIP,m_strIniPath,m_strport);
//if ( dwType != AFX_INET_SERVICE_HTTP )
//{
// //CHXcertinterfaceCtrl::m_Geterror = "連接的URL不是以HTTP://開頭";
// return 3;
//}
CString SendInfo = "client_id=Vuees&client_secret=secret&username=w001&password=123123&grant_type=password" ;
m_strIniPath = "https://idserver.xxxkj.com/connect/token?" ;
m_strIniPath = m_strIniPath + SendInfo;
m_cis.SetOption(INTERNET_OPTION_CONNECT_TIMEOUT,5);//用于指定要求的設定的因特網選項
m_cis.SetOption(INTERNET_OPTION_CLIENT_CERT_CONTEXT ,0);
m_pHttp=m_cis.GetHttpConnection(m_strIP,m_strport);//用于建立一個HTTP連接,回傳一個ChttpConnection函式
CHttpFile *pFile =m_pHttp->OpenRequest(CHttpConnection::HTTP_VERB_POST,
m_strIniPath, NULL, 1, NULL, NULL, INTERNET_FLAG_EXISTING_CONNECT); //打開HTTP的服務器連接
DWORD dwRet = 0;
DWORD szBuffer = 0;
DWORD dwBufferSize = sizeof(szBuffer);
try{
pFile->SendRequest(); //發送請求到HTTP
pFile->QueryInfo(HTTP_QUERY_STATUS_TEXT, szBuffer, &dwBufferSize) ;
pFile->QueryInfoStatusCode(dwRet);
}catch(CInternetException* pEx)
{
TCHAR szError[1024];
pEx->GetErrorMessage(szError,1024);
AfxMessageBox(szError);
pFile->Close();
}
if (dwRet == HTTP_STATUS_OK)
{
CString result, newline;
while(pFile->ReadString(newline))
{//回圈讀取每行內容
result += newline+"\r\n";
}
std::cout<<result<<std::endl;//顯示回傳內容
}
uj5u.com熱心網友回復:
總是回傳 404 這是回傳的結果<html>
<head><title>404 Not Found</title></head>
<body bgcolor="white">
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.14.1</center>
</body>
</html>
uj5u.com熱心網友回復:
我QQ 717807772uj5u.com熱心網友回復:
libcurl 非常好用也很簡單,一看就會,無非就是幾個函式curl_easy_init
curl_easy_setopt
curl_easy_perform
curl_easy_cleanup
uj5u.com熱心網友回復:
回傳404,這說明是服務端沒有找到網頁。你可以用瀏覽器直接訪問https://idserver.xxxkj.com/connect/token? 試試,看看回傳是什么。如果能正常訪問,應該是你拼接url的時候有錯誤了。如果顯示頁面沒找到,那就是服務端有問題,你先解決服務端的問題再除錯程式。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/12625.html
標籤:C++ 語言
上一篇:求助,C++ 如何讀取raw檔案資料,并且獲取資料的空間位置資訊
下一篇:有沒有C語言的速成方法
