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
";
stream << "Host: "<< host << "
";
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
";
stream << "Content-Type:application/x-www-form-urlencoded
";
stream << "Content-Length:" << post_content.length()<<"
";
stream << "Connection:close
";
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
";
stream << "Host: " << host << "
";
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
";
stream <<"Connection:close
";
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
";
stream += "Host: " + strHost + "
";
stream += "Cookie: "+ strCookie +"
";
stream += "Referer: "+ strReferer +"
";
stream += "Connection:close
";
//MessageBoxA(NULL , stream.c_str() ,"Hi" ,MB_OK);
return socketHttp(strHost, stream);
}