; char http_msg[4096] = ; strcpy(http_msg, "http://目標主機的ip和要傳送的請求資訊"); if(http->httpget(http_msg, http_return)) return 0; }
下面是標頭檔案,和cpp檔案放到同乙個資料夾
#define project_httpreq_h #endif //project_httpreq_h #include #include #include #include #include #include #include #include #include #include #include #include #include #define bufsize 41000 #define urlsize 1024 #define invalid_socket -1 #define __debug__ class httprequest ; int httprequest::httpget(const char* strurl, char* strresponse) /** * 功能描述:httppost請求 * 引數說明: * strurl: http請求url * strdata: post請求傳送的資料 * strresponse:http請求響應 * 返 回 值: * 1表示成功 * 0表示失敗 **/ int httprequest::httppost(const char* strurl, const char* strdata, char* strresponse) //執行http請求,get或post int httprequest::httprequestexec(const char* strmethod, const char* strurl, const char* strdata, char* strresponse) //限制url長度 if(urlsize < strlen(strurl)) //建立http協議頭 char* strhttphead = httpheadcreate(strmethod, strurl, strdata); //判斷套接字m_isocketfd是否有效,有效就直接傳送資料 if(m_isocketfd != invalid_socket) } } //create socket m_isocketfd = invalid_socket; m_isocketfd = socket(af_inet, sock_stream, 0); if (m_isocketfd < 0 ) //bind address and port int iport = getportfromurl(strurl); if(iport < 0) char* strip = getipfromurl(strurl); if(strip == null) struct sockaddr_in servaddr; bzero(&servaddr, sizeof(servaddr)); servaddr.sin_family = af_inet; servaddr.sin_port = htons(iport); if (inet_pton(af_inet, strip, &servaddr.sin_addr) <= 0 ) //set non-blocking int flags = fcntl(m_isocketfd, f_getfl, 0); if(fcntl(m_isocketfd, f_setfl, flags|o_nonblock) == -1) //非阻塞方式連線 int iret = connect(m_isocketfd, (struct sockaddr *)&servaddr, sizeof(servaddr)); if(iret == 0) else } else if(iret < 0) } iret = socketfdcheck(m_isocketfd); if(iret > 0) else } else return 1; } //構建http訊息頭 char* httprequest::httpheadcreate(const char* strmethod, const char* strurl, const char* strdata) ; unsigned ulen =
linux c 實現http的get 函式
使用linux c 實現了http的get請求 傳入引數 傳入引數是要請求的url 傳出引數 請求到的網頁 為了方便說明,這裡假定url 將網域名稱轉為ip位址 struct hostent host gethostbyname search.360buy.com char ipstr char i...
nodejs實現同步http請求
nodejs實現同步http模組請求。用到http模組。在命令列呼叫node命令執行,可以這樣使用 let http require http 引入http模組 let co require co http模組傳送請求 param host param port param route param ...
C 使用CHttpFile實現Http請求
c 實現http請求的 參照網上的修改了下在mfc中使用 操作成功 define success 0 操作失敗 define failure 1 操作超時 www.it165.net trymemset cword,0,num sizeof char 初始化 widechartomultibyte ...