int RiotAPI::getSite(std::string hostname) //RiotAPI is my class, almost empty atm
{
if ( wsa ) //wsa is true, dont mind this.
{
ZeroMemory( &hints, sizeof(hints) );
hints.ai_family = AF_UNSPEC;
hints.ai_protocol = IPPROTO_TCP;
hints.ai_socktype = SOCK_STREAM;
errcode = getaddrinfo( hostname.c_str(), HTTPPORT, &hints, &result ); //HTTPPORT defined as "80" <- string
if ( errcode != 0)
{
printf("getaddrinfo() failed error: %d\n", errcode);
}
else
printf("getaddrinfo success\n");
/*for ( ptr=result; ptr != NULL; ptr->ai_next )
{
cSock = socket(ptr->ai_family, ptr->ai_socktype, ptr->ai_protocol);
if ( cSock == INVALID_SOCKET )
{
printf("Socket creating failed.\n");
}
}*/
cSock = socket(result->ai_family, result->ai_socktype, result->ai_protocol); //didnt bother looping through results because the web address only returns 1 IP
errcode = connect(cSock, result->ai_addr, (int)result->ai_addrlen);
if ( errcode != 0 )
printf("Could not connect to the server");
else{
char request [] = "GET /api/lol/na/v1.1/summoner/by-name/RiotSchmick?api_key=<MY_API_KEY> HTTP/1.0\r\n";
send(cSock, request, (int)strlen(request), 0);
char output [256];
int bytesrecv = 0;
//char * cmp = "{\"id\":585897,\"name\":\"RiotSchmick\",\"profileIconId\":583,\"summonerLevel\":30,\"revisionDate\":1387143444000,\"revisionDateStr\":\"12/15/2013 09:37 PM UTC\"}";
bytesrecv = recv(cSock, output, 255, 0);
printf("Bytes read: %d\n", bytesrecv);
printf("Output string: %s\n", output);
}
}
return 0;
}我一直在尝试编写一个使用Riot (英雄联盟)的程序。
我的代码应该可以正常工作,我试图从我的朋友服务器获得一个网页,而且它成功了,不过是索引。(我使用了"GET HTTP/1.0\r\n")
如果我想得到这个网址的内容:
http://prod.api.pvp.net/api/lol/na/v1.1/summoner/by-name/RiotSchmick?api_key=<here_goes_my_api_key>我不该这样做吗?
1)连接到htt://prod.api.pvp.net
2)发送"GET /api/lol/na/v1.1/summoner/by-name/RiotSchmick?api_key= HTTP/1.0\r\n“
3) recv()直到返回0,然后打印出缓冲区(不过,我在代码中没有这样做,我只是尝试接收255个字节来查看它是否有效)
问题在于它只位于recv()函数(块?)上。我的GET请求有问题吗?
下面是API信息页面:http://i.imgur.com/yXr5BYx.png
我确实尝试在我的请求中使用标头,但是同样的事情,recv()就在那里。
我也试过这样做:
char buffer [2048];
recv(cSock, buffer, 2047, 0);
printf("Output string: %s\n", buffer); 此代码完整地返回页面(但并不是所有的2048字节都被填充):
而这只会返回3个随机的拼凑字符。
std::string output = "";
char buffer [128];
//int bytesrecv = 0;
while ( recv(cSock, buffer, 127, 0) > 0)
{
output += buffer;
}
printf("Output string: %s\n", output); 发布于 2013-12-16 21:04:52
你的GET请求没有完成,最低限度它需要用一个空行结束.即最后的\r\n\r\n。但是您可能会遇到其他问题,例如,如果服务器服务于多个域,则需要一个Host头.
https://stackoverflow.com/questions/20620229
复制相似问题