c++ - Winsock 400 错误请求,openresty
问题描述
所以我有一个用于我的软件的身份验证系统,我向我的服务器发送一个 GET 请求来验证它,然后回显真或假。当我使用 Winsock 发送 HTTP 请求时,我收到一个错误:400 bad request,低于它是 openresty。任何帮助表示赞赏!
#define _WIN32_WINNT 0x0400
#include <iostream>
#include <locale>
#include <sstream>
#include <vector>
#include <stdio.h>
#include <string.h>
#include <windows.h>
#include <winsock2.h>
#pragma comment(lib, "ws2_32.lib")
using namespace std;
void main() {
WSADATA wsaData;
SOCKET Socket;
SOCKADDR_IN SockAddr;
int lineCount = 0;
int rowCount = 0;
struct hostent* host;
locale local;
char buffer[10000];
int i = 0;
int nDataLength;
string website_HTML;
// website url
string url = "www.mysitewebsite.com/auth/auth.php?hwid=" + hwidPro +
"&username=" + Username + "&password=" + Password;
// HTTP GET
string get_http =
"GET / HTTP/1.1\r\nHost: " + url + "\r\nConnection: close\r\n\r\n";
if(WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) {
cout << "WSAStartup failed.\n";
system("pause");
// return 1;
}
Socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
host = gethostbyname("www.mysite.com");
SockAddr.sin_port = htons(80);
SockAddr.sin_family = AF_INET;
SockAddr.sin_addr.s_addr = *((unsigned long*)host->h_addr);
if(connect(Socket, (SOCKADDR*)(&SockAddr), sizeof(SockAddr)) != 0) {
cout << "Could not connect";
system("pause");
// return 1;
}
// send GET / HTTP
send(Socket, get_http.c_str(), strlen(get_http.c_str()), 0);
// recieve html
while((nDataLength = recv(Socket, buffer, 10000, 0)) > 0) {
int i = 0;
while(buffer[i] >= 32 || buffer[i] == '\n' || buffer[i] == '\r') {
website_HTML += buffer[i];
i += 1;
}
}
closesocket(Socket);
WSACleanup();
const char* result = website_HTML.c_str();
Log::Msg(result);
if(website_HTML == "true") {
authsucsess();
} else {
Log::Fatal(result);
Log::Fatal("Invalid Login or HWID");
exit(0);
}
}
我已将我的帖子编辑为一个最小的、可重复的示例。希望它更好 编辑 2:我添加了 HTTP 响应:
HTTP/1.1 400 Bad Request
Date: Sat, 22 Jun 2019 21:33:11 GMT
Content-type: text/html
content-length: 170
connection: close
Server: awex
X-Xss Protection: 1; mode=block
X-Content-Type-Options: nosniff
X-Request-ID: 7ccdf09347f06d7a061ad41aa45d5af7
<html>
<head><title>400 Bad Request</title></head>
<body bgcolor="white">
<center><h1>400 Bad Request</h1></center>
<hr><center>openresty</center>
</body>
</html>
解决方案
您的GET
请求格式不正确。这是您要发送的内容:
GET / HTTP/1.1
Host: www.mysitewebsite.com/auth/auth.php?hwid=[...]&username=[...]&password=[...]
Connection: close
它需要看起来像这样:
GET /auth/auth.php?hwid=[...]&username=[...]&password=[...] HTTP/1.1
Host: www.mysitewebsite.com
Connection: close
尝试这个:
string s_host = "www.mysitewebsite.com";
string s_resource = "/auth/auth.php";
string s_query = "?hwid=" + hwidPro + "&username=" + Username + "&password=" + Password;
string url = s_host + s_resource + s_query;
string get_http = "GET " + s_resource + s_query + " HTTP/1.1\r\n"
"Host: " + s_host + "\r\n"
"Connection: close\r\n"
"\r\n";
...
host = gethostbyname(s_host.c_str());
...
send(Socket, get_http.c_str(), get_http.size(), 0);
...
while((nDataLength = recv(Socket, buffer, sizeof(buffer), 0)) > 0)
{
website_HTML.append(buffer, nDataLength);
}
...
推荐阅读
- php - 如何将图像放在php的下拉行中
- terraform - Terraform 无法在 openstack 提供程序中创建 VM(在服务目录中找不到合适的端点)
- oracle - 如何从 oracle forms 11g 打开 PDF 文件?
- css - 猫头鹰旋转木马在最后创造了额外的空间
- javascript - 用视频代替
- c++ - 派生类指针使用 Static_Cast 指向基类对象
- d3.js - d3 mouseover tip.show 现在在函数中工作(a,b,c)
- node.js - child_process 未在 ec2 nodejs 服务器上执行
- ios - 如果我在 uicollection 视图的最后一页,如何转到下一个视图控制器。它的水平转换和分页启用
- xml - ejabberd 在错误的 XML 消息上断开连接