- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我的 Minecraft 服务器程序遇到了这个讨厌的问题。当您尝试登录到我的 minecraft 服务器时,我尝试从 minecraft.com 获得登录响应。一切都很好,并设置了验证 token key 和共享 key 。
我只是无法获得使用 curl 的 get 请求 :(
这是我的:
requestString.append("GET /session/minecraft/hasJoined?username=");
requestString.append(player.Username);
requestString.append("&");
requestString.append("serverId=");
requestString.append(player.loginHash);
requestString.append(" HTTP/1.0\r\n");
std::cout << requestString << std::endl;
curl_easy_setopt(curl, CURLOPT_URL, "https://sessionserver.mojang.com");
list = curl_slist_append(list, requestString.c_str());
list = curl_slist_append(list, "User-Agent: MinecraftServerPP\r\n");
list = curl_slist_append(list, "Connection: close\r\n");
list = curl_slist_append(list, "\r\n");
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, list);
curl_easy_setopt(curl, CURLOPT_CAINFO, "cacert.pem");
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &_write_data);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &response);
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
我从服务器得到一个 ok,但不是包含我需要的数据的 json 对象。
最佳答案
为什么要将 GET
请求行放在 HTTP header 中?它根本不属于那里,事实上 libCURL 文档甚至指出了这一点:
The first line in a request (containing the method, usually a GET or POST) is not a header and cannot be replaced using this option. Only the lines following the request-line are headers. Adding this method line in this list of headers will only cause your request to send an invalid header. Use CURLOPT_CUSTOMREQUEST to change the method.
您当前的代码正在发送与此类似的 GET
请求:
GET / HTTP/1.1Host: sessionserver.mojang.comGET /session/minecraft/hasJoined?username=<username>&serverId=<loginHash> HTTP/1.0User-Agent: MinecraftServerPPConnection: close
Which is why you are not getting the response you are expecting. You are not actually requesting the /session/minecraft/hasJoined
resource, you are actually requesting the root /
resource.
You should be sending a GET
request like this instead:
GET /session/minecraft/hasJoined?username=<username>&serverId=<loginHash> HTTP/1.0Host: sessionserver.mojang.comUser-Agent: MinecraftServerPPConnection: close
To accomplish that, you need to pass the complete URL to CURLOPT_URL
and let libCURL handle the GET
line for you. You can use CURLOPT_HTTPGET
to ensure that GET
is actually used.
Also, you should be using CURLOPT_HTTP_VERSION
if you want to specify a specific HTTP version.
Also, although you can use CURLOPT_HTTPHEADER
for the User-Agent
and Connection
headers, you should be using CURLOPT_USERAGENT
and CURLOPT_FORBID_REUSE
instead.
Try this instead:
url = "https://sessionserver.mojang.com/session/minecraft/hasJoined?username=");
url.append(player.Username); // <-- NOTE: see further below!
url.append("&serverId=");
url.append(player.loginHash); // <-- NOTE: see further below!
std::cout << url << std::endl;
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
curl_easy_setopt(curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L);
curl_easy_setopt(curl, CURLOPT_FORBID_REUSE, 1L);
curl_easy_setopt(curl, CURLOPT_CAINFO, "cacert.pem");
curl_easy_setopt(curl, CURLOPT_USERAGENT, "MinecraftServerPP");
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &_write_data);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &response);
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
现在,话虽如此,您需要考虑额外的逻辑才能正确处理 CURLOPT_URL
:
Pass in a pointer to the URL to work with. The parameter should be a char * to a zero terminated string which must be URL-encoded in the following format:
scheme://host:port/path
For a greater explanation of the format please see RFC 3986.
libcurl doesn't validate the syntax or use this variable until the transfer is issued. Even if you set a crazy value here,
curl_easy_setopt
will still returnCURLE_OK
.
特别是,RFC 的这些部分与您的代码相关:
2.1. Percent-Encoding
A percent-encoding mechanism is used to represent a data octet in a component when that octet's corresponding character is outside the allowed set or is being used as a delimiter of, or within, the component. A percent-encoded octet is encoded as a character triplet, consisting of the percent character "%" followed by the two hexadecimal digits representing that octet's numeric value. For example, "%20" is the percent-encoding for the binary octet "00100000" (ABNF: %x20), which in US-ASCII corresponds to the space character (SP). Section 2.4 describes when percent-encoding and decoding is applied.
pct-encoded = "%" HEXDIG HEXDIG
The uppercase hexadecimal digits 'A' through 'F' are equivalent to the lowercase digits 'a' through 'f', respectively. If two URIs differ only in the case of hexadecimal digits used in percent-encoded octets, they are equivalent. For consistency, URI producers and normalizers should use uppercase hexadecimal digits for all percent-encodings.
2.4. When to Encode or Decode
Under normal circumstances, the only time when octets within a URI are percent-encoded is during the process of producing the URI from its component parts. This is when an implementation determines which of the reserved characters are to be used as subcomponent delimiters and which can be safely used as data. Once produced, a URI is always in its percent-encoded form.
When a URI is dereferenced, the components and subcomponents significant to the scheme-specific dereferencing process (if any) must be parsed and separated before the percent-encoded octets within those components can be safely decoded, as otherwise the data may be mistaken for component delimiters. The only exception is for percent-encoded octets corresponding to characters in the unreserved set, which can be decoded at any time. For example, the octet corresponding to the tilde ("~") character is often encoded as "%7E" by older URI processing implementations; the "%7E" can be replaced by "~" without changing its interpretation.
Because the percent ("%") character serves as the indicator for percent-encoded octets, it must be percent-encoded as "%25" for that octet to be used as data within a URI. Implementations must not percent-encode or decode the same string more than once, as decoding an already decoded string might lead to misinterpreting a percent data octet as the beginning of a percent-encoding, or vice versa in the case of percent-encoding an already percent-encoded string.
URL 的 query
组件在 section 3.4 中定义:
query = *( pchar / "/" / "?" )
pchar
在 section 3.3 中定义:
pchar = unreserved / pct-encoded / sub-delims / ":" / "@"
unreserved
在 section 2.3 中定义:
unreserved = ALPHA / DIGIT / "-" / "." / "_" / "~"
sub-delims
在 section 2.2 中定义:
sub-delims = "!" / "$" / "&" / "'" / "(" / ")" / "*" / "+" / "," / ";" / "="
因此,如果您的 player.Username
和 player.loginHash
字符串中有任何字符在这些允许的字符之外,您必须在将它们放入 URL 的查询字符串时对它们进行百分比编码。例如:
std::string encodeForUrlQuery(std::string s)
{
static const char* allowed = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-._~!$&'()*+,;=:@/?";
// this is just an example, in practice you should first convert the
// input string to Unicode and charset-encode it, usually to UTF-8,
// and then percent-encode the resulting octets...
std::string::size_type idx = s.find_first_not_of(allowed);
while (idx != std::string::npos)
{
std::ostringstream oss;
oss << '%' << std::hex << std::setw(2) << std::setfill('0') << std::uppercase << (int)s[idx];
std::string encoded = oss.str();
s.replace(idx, 1, encoded);
idx = s.find_first_not_of(allowed, idx+encoded.length());
}
return s;
}
url = "https://sessionserver.mojang.com/session/minecraft/hasJoined?username=");
url.append(encodeForUrlQuery(player.Username));
url.append("&serverId=");
url.append(encodeForUrlQuery(player.loginHash));
std::cout << url << std::endl;
关于c++ - 使用 CURL 进行 GET 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37997328/
我正在尝试从该网站抓取历史天气数据: http://www.hko.gov.hk/cis/dailyExtract_uc.htm?y=2016&m=1 在阅读了 AJAX 调用后,我发现请求数据的正确
我有两个 postman 请求 x,y,它们命中了两个不同的休息 api X,Y 中的端点。 x 会给我一个身份验证 token ,这是发出 y 请求所必需的。如何在请求 y 中发出请求 x ?也就是
我使用请求库通过 API 与其他服务器进行通信。但现在我需要同时发送多个(10 个或更多)POST 请求,并且只有在所有响应都正确的情况下才能进一步前进。通常语法看起来有点像这样: var optio
背景:当用户单击按钮时,其类会在class1和class2之间切换,并且此数据是通过 AJAX 提交。为了确认此数据已保存,服务器使用 js 进行响应(更新按钮 HTML)。 问题:如果用户点击按钮的
我正在将 Node.js 中的请求库用于 Google 的文本转语音 API。我想打印出正在发送的请求,如 python example . 这是我的代码: const request = requi
我经常使用requests。最近我发现还有一个 requests2 和即将到来的 requests3 虽然有一个 page其中简要提到了 requests3 中的内容,我一直无法确定 requests
我正在尝试将图像发送到我的 API,然后从中获取结果。例如,我使用发送一个 bmp 图像文件 file = {"img": open("img.bmp)} r = requests.post(url,
我发现 Google Cloud 确保移出其物理环境的任何请求都经过强制加密,请参阅(虚拟机到虚拟机标题下的第 6 页)this link Azure(和 AWS)是否遵循类似的程序?如果有人能给我指
我有一个 ASP.NET MVC 应用程序,我正在尝试在 javascript 函数中使用 jQuery 来创建一系列操作。该函数由三部分组成。 我想做的是:如果满足某些条件,那么我想执行同步 jQu
我找不到如何执行 get http 请求,所以我希望你们能帮助我。 这个想法是从外部url(例如 https://api.twitter.com/1.1/search/tweets.json?q=tw
我的应用只需要使用“READ_SMS”权限。我的问题是,在 Android 6.0 上,当我需要使用新的权限系统时,它会要求用户“发送和查看短信”。 这是我的代码: ActivityCompat.re
我的前端代码: { this.searchInput = input; }}/> 搜索 // search method: const baseUrl = 'http://localho
我有一个由 AJAX 和 C# 应用程序使用的 WCF 服务, 我需要通过 HTTP 请求 header 发送一个参数。 在我的 AJAX 上,我添加了以下内容并且它有效: $.ajax({
我正在尝试了解如何使用 promises 编写代码。请检查我的代码。这样对吗? Node.js + 请求: request(url, function (error, response, body)
如果失败(除 HTTP 200 之外的任何响应代码),我需要重试发送 GWT RPC 请求。原因很复杂,所以我不会详细说明。到目前为止,我在同一个地方处理所有请求响应,如下所示: // We
当用户单击提交按钮时,我希望提交表单。然而,就在这种情况发生之前,我希望弹出一个窗口并让他们填写一些数据。一旦他们执行此操作并关闭该子窗口,我希望发出 POST 请求。 这可能吗?如果可能的话如何?我
像 Facebook 这样的网站使用“延迟”加载 js。当你必须考虑到我有一台服务器,流量很大时。 我很感兴趣 - 哪一个更好? 当我一次执行更多 HTTP 请求时 - 页面加载速度较慢(由于限制(一
Servlet 容器是否创建 ServletRequest 和 Response 对象或 Http 对象?如果是ServletRequest,谁在调用服务方法之前将其转换为HttpServletReq
这是维基百科文章的摘录: In contrast to the GET request method where only a URL and headers are sent to the serv
我有一个循环,每次循环时都会发出 HTTP post 请求。 for(let i = 1; i console.log("succes at " + i), error => con
我是一名优秀的程序员,十分优秀!