- .h头文件
#pragma once
#ifndef CHTTP
#define CHTTP
#define _WINSOCK_DEPRECATED_NO_WARNINGS
#include <winsock2.h>
#include <string>
#include <map>
#include <iostream>
#include <sstream>
using namespace std;
class CHttp
{
public:
CHttp(string domainName, int port);
int SetHeader(string strKey, string strValue);
int GetHeader(string strKey, string &strValue);
int GET(string source);
int POST(string source, string data);
string getResponseData();
private:
map<string,string> headerMap;
string DomainName;
string SourcePos;
string Cookie;
string ResponseData;
SOCKET Socket;
int Port;
int InitEnv();
void setSource(string source);
string getCookie(string header);
int Call(string method, string source, string data);
};
// 测试例子
void HttpDemo();
#endif // !CHTTP
2.cpp实现文件
#include "CHttp.h"
#include "CConvert.h"
#define HEAD_Content_Type "Content-Type"
#define HEAD_Content_Type_Form "application/x-www-form-urlencoded; charset=utf-8"
#define HEAD_Content_Type_Json "application/json; charset=utf-8"
#define HEAD_Content_Type_Xml "application/xml; charset=utf-8"
CHttp::CHttp(string domainName, int port)
{
this->Cookie = "";
WSADATA WSAData;
WSAStartup(MAKEWORD(2, 2), &WSAData);
this->DomainName = domainName;
this->Port = port;
}
int CHttp::SetHeader(string strKey, string strValue) {
this->headerMap[strKey] = strValue;
return 1;
}
int CHttp::GetHeader(string strKey, string& strValue) {
map<string, string>::iterator it = this->headerMap.begin();
string s_value;
it = headerMap.find(strKey);
if (it != headerMap.end()) {
s_value = it->second;
}
// 设置默认值
if (strKey == HEAD_Content_Type) {
if (s_value == "") {
s_value = HEAD_Content_Type_Form;
}
}
strValue = s_value;
return 1;
}
int CHttp::InitEnv()
{
int ret;
this->Socket = socket(AF_INET, SOCK_STREAM, 0);
hostent* host = gethostbyname(this->DomainName.c_str());
sockaddr_in saddr;
saddr.sin_family = AF_INET;
saddr.sin_port = htons(this->Port);
memcpy(&saddr.sin_addr, host->h_addr, 4);
if (connect(this->Socket, (sockaddr*)&saddr, sizeof(saddr)) == -1)
{
ret = -1;
}
else
{
ret = 1;
}
int timeout = 3000; //3s //设置发送数据和接收数据的最大延迟时间
setsockopt(this->Socket, SOL_SOCKET, SO_SNDTIMEO, (char*)&timeout, sizeof(timeout));
setsockopt(this->Socket, SOL_SOCKET, SO_RCVTIMEO, (char*)&timeout, sizeof(timeout));
return ret;
}
void CHttp::setSource(string source)
{
this->SourcePos = source;
}
string CHttp::getResponseData()
{
char buf[1024];
int n;
int sum = 0;
int find;
int start;
const string splitStr = "\r\n\r\n";
string resiveData;
CConvert cct;
this->ResponseData = "";
// 正在获取html
while ((n = recv(this->Socket, buf, sizeof(buf) - sizeof(char), 0)) != 0)
{
for (int i = 0; i < n; i++)
{
resiveData += buf[i];
}
sum++;
if (sum == 10)
break;
}
closesocket(this->Socket);
// 截取有用的返回值
find = resiveData.find(splitStr);
start = find + splitStr.length();
resiveData = resiveData.substr(start, resiveData.length() - start);
this->ResponseData = cct.Utf8ToGbk(resiveData.c_str());
return this->ResponseData;
}
string CHttp::getCookie(string header)
{
int begin = header.find("Cookie:");
int end = header.find("path", begin);
if ((begin > 0) && (end > begin)){
int len = end - begin;
string cookie = header.substr(begin, len);
this->Cookie = cookie;
}
return this->Cookie;
}
int CHttp::POST(string source, string data)
{
int ret;
ret = this->Call("POST", source,data);
return ret;
}
int CHttp::GET(string source)
{
int ret;
ret = this->Call("GET", source, "");
return ret;
}
int CHttp::Call(string method, string source, string data) {
int ret;
string contentType;
CConvert cct;
ret = this->InitEnv();
// 初始化失败
if (ret == -1) {
return ret;
}
// 转成UTF-8格式
source = cct.GbkToUtf8(source);
this->setSource(source);
// 转成UTF-8格式
if (data != "") {
data = cct.GbkToUtf8(data);
}
stringstream request;
request << method + " " << this->SourcePos << " HTTP/1.1\r\n";
request << "Host:" << this->DomainName << "\r\n";
if (data != "") {
request << "Content-Length:" << data.length() << "\r\n";//+ "\r\n";
}
request << "Connection:close\r\n";
if (this->Cookie != "")
request << this->Cookie + "\r\n";
// 获取类型
this->GetHeader(HEAD_Content_Type, contentType);
request << "Content-Type:"<< contentType <<"\r\n";
request << "\r\n";
request << data;
// 发送
string Post = request.str();
ret = send(this->Socket, Post.c_str(), Post.size(), 0);
if (ret > 0) {
return 1;
}
else {
return -1;
}
}
void HttpDemo() {
string html;
int ret;
CHttp CHttp("127.0.0.1", 8080);
// get请求
CHttp.SetHeader(HEAD_Content_Type,HEAD_Content_Type_Form);
ret = CHttp.GET("/get?name=123");
if (ret == 1) {
html = CHttp.getResponseData();
cout << html << endl;
}
// post请求
ret = CHttp.POST("/post", "name=234");
if (ret == 1) {
html = CHttp.getResponseData();
cout << html << endl;
}
}
3.java 内服务
@GetMapping("/get")
public String get(String name){
return "Get Hello World " + name;
}
@PostMapping("/post")
public String post(String name){
return "Post Hello World " + name;
}
4.运行效果