1. 程式人生 > >HTTP自定義Header-(SOCKET-TCP)

HTTP自定義Header-(SOCKET-TCP)

  HTTP自定義Header-TCP

    前幾天弄一些東西,需要在傳送http請求的時候自定義http頭,找了幾個庫用著很不爽。有的把Cookie直接幹掉了,還自己在頭裡加了版權,最後終於忍不了了。在網上一把梭了一個TCP的,這個比較靈活,沒有那麼多事,THHP下面終歸還是TCP:

一把梭的程式碼如下:

#include "HttpConnect.h"

#ifdef WIN32
#pragma comment(lib,"ws2_32.lib")
#endif

HttpConnect::HttpConnect()
{
#ifdef WIN32
    //此處一定要初始化一下,否則gethostbyname返回一直為空
    WSADATA wsa = { 0 };
    WSAStartup(MAKEWORD(2, 2), &wsa);
#endif
}

HttpConnect::~HttpConnect()
{

}
void HttpConnect::socketHttp(std::string host, std::string request)
{
    int sockfd;
    struct sockaddr_in address;
    struct hostent *server;

    sockfd = socket(AF_INET,SOCK_STREAM,0);
    address.sin_family = AF_INET;
    address.sin_port = htons(80);
    server = gethostbyname(host.c_str());
    memcpy((char *)&address.sin_addr.s_addr,(char*)server->h_addr, server->h_length);

    if(-1 == connect(sockfd,(struct sockaddr *)&address,sizeof(address))){
        DBG <<"connection error!"<<std::endl;
        return;
    }

    DBG << request << std::endl;
#ifdef WIN32
    send(sockfd, request.c_str(),request.size(),0);
#else
    write(sockfd,request.c_str(),request.size());
#endif
    char buf[1024*1024] = {0};


    int offset = 0;
    int rc;

#ifdef WIN32
    while(rc = recv(sockfd, buf+offset, 1024,0))
#else
    while(rc = read(sockfd, buf+offset, 1024))
#endif
    {
        offset += rc;
    }

#ifdef WIN32
    closesocket(sockfd);
#else
    close(sockfd);
#endif
    buf[offset] = 0;
    DBG << buf << std::endl;

}

void HttpConnect::postData(std::string host, std::string path, std::string post_content)
{
    //POST請求方式
    std::stringstream stream;
    stream << "POST " << path;
    stream << " HTTP/1.0\r\n";
    stream << "Host: "<< host << "\r\n";
    stream << "User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.2.3) Gecko/20100401 Firefox/3.6.3\r\n";
    stream << "Content-Type:application/x-www-form-urlencoded\r\n";
    stream << "Content-Length:" << post_content.length()<<"\r\n";
    stream << "Connection:close\r\n\r\n";
    stream << post_content.c_str();

    socketHttp(host, stream.str());
}

void HttpConnect::getData(std::string host, std::string path, std::string get_content)
{
    //GET請求方式
    std::stringstream stream;
    stream << "GET " << path << "?" << get_content;
    stream << " HTTP/1.0\r\n";
    stream << "Host: " << host << "\r\n";
    stream <<"User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.2.3) Gecko/20100401 Firefox/3.6.3\r\n";
    stream <<"Connection:close\r\n\r\n";

    socketHttp(host, stream.str());
}
 

呼叫方法:

    HttpConnect *http = new HttpConnect();
    http->getData("127.0.0.1", "/login", "id=liukang&pw=123");
    http->postData("127.0.0.1", "/login","id=liukang&pw=123");
	
    當時用的是GET 自動以Header,就順便改了下,程式碼如下。用的時候可以根據實際情況,改改上面程式碼就行了。TCP的話在C++裡控制Header就比較方便了。
HHHHHH.h	
#pragma once
#define WIN32_LEAN_AND_MEAN

#include <windows.h>
#include <winsock2.h>
#include <ws2tcpip.h>
#include <stdio.h>
#include <string>
#include <iostream>
#include <sstream>

#pragma comment(lib,"ws2_32.lib")
#pragma warning(disable:4996)

class HttpConnect{
public:
	HttpConnect();
	~HttpConnect();
	std::string socketHttp(std::string host, std::string request);
	std::string getData(std::string strHost, std::string strPath, std::string strValue,
	std::string strCookie, std::string strReferer
	);
};

CPPPPPP.cpp

#include "stdafx.h"
#include "HttpConnect.h"

HttpConnect::HttpConnect(){
#ifdef WIN32
	WSADATA wsa = { 0 };
	WSAStartup(MAKEWORD(2, 2), &wsa);
#endif
}

HttpConnect::~HttpConnect(){

}

char buf[1024 * 1024] = { 0 };//放外面,陣列太大,放函式裡,棧可能不夠大。

std::string HttpConnect::socketHttp(std::string host, std::string request){
	int sockfd;
	struct sockaddr_in address;
	struct hostent *server;
	sockfd = socket(AF_INET, SOCK_STREAM, 0);
	address.sin_family = AF_INET;
	address.sin_port = htons(80);
	server = gethostbyname(host.c_str());
	memcpy((char *)&address.sin_addr.s_addr, (char*)server->h_addr, server->h_length);

	if (-1 == connect(sockfd, (struct sockaddr *)&address, sizeof(address))) {
		//DBG << "connection error!" << std::endl;
		return "";
	}
	//MessageBoxA(NULL, request.c_str(), host.c_str(), MB_OK);
#ifdef WIN32
	send(sockfd, request.c_str(), request.size(), 0);
#else
	write(sockfd, request.c_str(), request.size());
#endif
	//char buf[1024 * 1024] = { 0 };

	int offset = 0;
	int rc;

#ifdef WIN32
	while (rc = recv(sockfd, buf + offset, 1024, 0))
#else
	while (rc = read(sockfd, buf + offset, 1024))
#endif
	{
		offset += rc;
	}

#ifdef WIN32
	closesocket(sockfd);
#else
	close(sockfd);
#endif
	buf[offset] = 0;
	//MessageBoxA(NULL , buf ,"Hi" ,MB_OK);
	return std::string(buf);
}

std::string HttpConnect::getData(std::string strHost, std::string strPath, std::string strValue,
	                              std::string strCookie , std::string strReferer
) {
	//GET請求方式
	std::string stream = "";
	stream += "GET " + strPath + "?" + strValue;
	stream += " HTTP/1.0\r\n";
	stream += "Host: " + strHost + "\r\n";
	stream += "Cookie: "+ strCookie +"\r\n";
	stream += "Referer: "+ strReferer +"\r\n";
	stream += "Connection:close\r\n\r\n";
	//MessageBoxA(NULL , stream.c_str() ,"Hi" ,MB_OK);
	return socketHttp(strHost, stream);
}