回复帖子请求

时间:2016-03-27 03:49:26

标签: c++ sockets post

我发送了一个帖子请求

$.ajax({
  url: "http://localhost:5002",
  type: "POST",
  async: false,
  cache: false,
  data: "username=" +  "heyyyys",
  success: function(response) {
    alert(response);
    return response;        
  }
});

我已成功接受请求

int result = recv(server, buffer, bufSize, 0);
cout << buffer << endl;

这里是缓冲区:

POST / HTTP/1.1
Host: localhost:5002
Connection: keep-alive
Content-Length: 16
Accept: */*
Origin: http://localhost
User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.87 Safari/537.36
Content-Type: application/x-www-form-urlencoded; charset=UTF-8
Referer: http://localhost/osrm/main.html
Accept-Encoding: gzip, deflate
Accept-Language: ru-RU,ru;q=0.8,en-US;q=0.6,en;q=0.4

username=heyyyys

我正在尝试回答

response << "POST / HTTP/1.1\r\n"
<< "HOST: localhost:5002\r\n"
<< "User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.87 Safari/537.36"
<< "Content-Type: application/x-www-form-urlencoded; charset=UTF-8\r\n"
<< "Accept: */*\r\n"
<< "Content-Length: 4\r\n"
<< "Connection: close\r\n\r\n";
<< "resp";
send(server, response.str().c_str(), response.str().length(), 0);

但是不可能回答。 告诉我我的错误。

1 个答案:

答案 0 :(得分:0)

谢谢你, 我在chrome dev工具中看到了发布请求的示例,并根据这样的响应生成了:

HTTP/1.1 200 OK
Date: Sun, 10 Oct 2010 23:26:07 GMT
Accept-Ranges: bytes
Content-Length: 11
Access-Control-Allow-Origin: *
Connection: close
Content-Type: text/html; charset=UTF-8

hello world!