利用c++語言+socket實現HTTP請求,請求獲得的數據效果圖如下:
HTTP協議的下一層是TCP,根據HTTP協議只需要利用TCP發送下面的數據到達目標主機,目標主機就會發送相應的數據到客戶端。
代碼截圖:
特別注意每一行末的\r\n
下面是代碼實現:
//mySocket.h頭文件#include <Winsock2.h> #include <Windows.h> #include <Ws2tcpip.h> #pragma comment(lib, "ws2_32.lib") #include <string> using namespace std; //只是對Socket中使用TCP協議的一種封裝 class MySocket{ public: static int InitClient(SOCKET *sock, string ip, int port); static int CloseMySocket(SOCKET *Sock); static int SendData(SOCKET sock, const string data); static int RecvData(SOCKET sock, string &data); }; View Code
//mySocket.c源碼文件1 #include "mySocket.h" 2 3 //只是對Socket中使用TCP協議的封裝 4 5 int MySocket::InitClient(SOCKET *sock, string ip, int port) 6 { 7 WSADATA wsaData;//初始化wsaData 8 if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) 9 { 10 return -1; 11 } 12 13 //創建套接字 14 if ((*sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) == INVALID_SOCKET) 15 { 16 WSACleanup(); 17 return -1; 18 } 19 20 struct sockaddr_in serverAddr; 21 serverAddr.sin_family = AF_INET; 22 serverAddr.sin_port = htons(port); 23 serverAddr.sin_addr.s_addr = inet_addr(ip.c_str()); 24 25 if (connect(*sock, (struct sockaddr *)&serverAddr, sizeof(serverAddr)) == SOCKET_ERROR) 26 { 27 return -1; 28 } 29 30 return 0; 31 } 32 33 int MySocket::CloseMySocket(SOCKET *sock) 34 { 35 if (closesocket(*sock) == SOCKET_ERROR) 36 { 37 WSACleanup(); 38 return -1; 39 } 40 return 0 ; 41 } 42 43 int MySocket::RecvData(SOCKET sock, string &data) 44 { 45 int bufLen = 255; 46 char buf[256]; 47 int recvLen= 0; 48 int iResult; 49 buf[bufLen] = '\0'; 50 while (true) 51 { 52 iResult = recv(sock, buf, bufLen, 0); 53 if (iResult < 0) 54 { 55 data = ""; 56 return -1; 57 } 58 59 recvLen += iResult; 60 61 if (iResult == 0) 62 { 63 return recvLen; 64 } 65 66 if (iResult == bufLen) 67 { 68 data += buf; 69 ZeroMemory(buf, bufLen); 70 continue; 71 } 72 if (iResult > 0 && iResult < bufLen) 73 { 74 data += buf; 75 return recvLen; 76 } 77 } 78 } 79 80 int MySocket::SendData(SOCKET sock, const string data) 81 { 82 int iResult = send(sock, data.c_str(), data.length(), 0); 83 if (iResult == SOCKET_ERROR) { 84 MySocket::CloseMySocket(&sock); 85 WSACleanup(); 86 return -1; 87 } 88 89 return 0; 90 } View Code
//main函數
#include <stdio.h> #include <iostream> #include<time.h> #include "mySocket.h" using namespace std; int main() { SOCKET clientSock; string str; int iResult; if (MySocket::InitClient(&clientSock, "42.121.254.229", 80) == -1)//主機IP地址+端口號 { printf("連接失敗\n"); return -1; } string head = "GET / HTTP/1.1\r\n"; head.append("Host: m.cnblogs.com\r\n");//請求的域名 head.append("Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8\r\n"); head.append("User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2062.120 Safari/537.36\r\n"); head.append("Accept-Language: zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3\r\n"); head.append("Accept-Charset: GB2312,utf-8;q=0.7,*;q=0.7"); head.append("Accept-Encoding: gzip,deflate\r\n"); head.append("\r\n");//表明請求頭結束了 iResult = MySocket::SendData(clientSock, head); if (iResult == -1) { printf("發送數據失敗\n"); return -1; } iResult = MySocket::RecvData(clientSock, str); if (iResult == -1) { printf("接受數據失敗\n"); return -1; } printf("----接受數據長度:%d----\n", iResult); printf(str.c_str()); MySocket::CloseMySocket(&clientSock); return 0; }
如果請求別的網站,有可能連返回的數據全部是亂碼,那就是網站啟用了gzip壓縮了。
圖中截圖中文亂碼,是因為返回的是UTF-8編碼的字符,控制台默認是已GBK編碼顯示的。
72.HTTP下載
//HttpDownload.h
#ifndef _HTTPDOWNLOAD_H
#define _HTTPDOWNLOAD_H
//#include "InitSock.h"
//static CInitSock initsock;
/************************************************************************
Description: Base64編碼、解碼類
Function: 可用於解決http協議的授權驗證問題
************************************************************************/
class CBase64
{
private:
CBase64();
~CBase64();
CBase64(const CBase64&);
CBase64& operator = (const CBase64&);
public:
static int Base64Encode(LPCTSTR lpszEncoding, CString& strEncoded);
static int Base64Decode(LPCTSTR lpszDecoding, CString& strDecoded);
public:
static int s_nBase64Mask[];
static CString s_strBase64TAB;
};
/************************************************************************
Description: 用http協議下載文件
Function: 支持http下載文件,用socket實現, 簡單的非阻塞超時機制
************************************************************************/
class CHttpDownload
{
public:
CHttpDownload();
~CHttpDownload();
public:
enum
{
DEFAULT_PORT = 80 ,
DEFAULT_CONNECTTIMEOUT = 10,
DEFAULT_SENDTIMEOUT = 10,
DEFAULT_RECVTIMEOUT = 10,
};
bool Download(LPCTSTR lpszDownloadUrl, LPCTSTR lpszSavePath);
protected:
bool CreateSocket();
void CloseSocket();
void FormatRequestHeader(const char* pszServer, int nPort, const char* pszObject, const char *pAuthorization = NULL);
bool SendRequest();
private:
bool Connect(const char* pszHostIP, int nPort = DEFAULT......余下全文>>
Java版本
1.編寫服務器端,實例化ServerSocket對象,這裡要定義ip和端口,實例化Socket,從ServerSocket對象中accept() 獲取,這裡要涉及到流,輸入輸出流在Socket中獲取
2.編寫客戶端,實例化Socket對象,ip,端口,流,在Socket中獲取
如果想要具體代碼,就留言,有空發給你