我们如何通过GET方法发送数据?

发布于 2024-08-13 15:50:31 字数 511 浏览 4 评论 0原文

我正在创建 HTTPS 连接并将请求属性设置为 GET:

_httpsConnection = (HttpsConnection) Connector.open(URL, Connector.READ_WRITE);
_httpsConnection.setRequestMethod(HttpsConnection.GET);

但是如何发送 GET 参数? 我是否像这样设置请求属性:

_httpsConnection.setRequestProperty("method", "session.getToken");
_httpsConnection.setRequestProperty("developerKey", "value");
_httpsConnection.setRequestProperty("clientID", "value");

或者我是否必须写入连接的输出流?

或者我是否需要通过将参数/值附加到 url 来发送参数/值?

I am creating a HTTPS connection and setting the request property as GET:

_httpsConnection = (HttpsConnection) Connector.open(URL, Connector.READ_WRITE);
_httpsConnection.setRequestMethod(HttpsConnection.GET);

But how do I send the GET parameters?
Do I set the request property like this:

_httpsConnection.setRequestProperty("method", "session.getToken");
_httpsConnection.setRequestProperty("developerKey", "value");
_httpsConnection.setRequestProperty("clientID", "value");

or do I have to write to the output stream of the connection?

or do I need to send the Parameter/Values by appending it to the url?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

澜川若宁 2024-08-20 15:50:31

调用 Connection.setRequestProperty() 将设置请求标头,在这种情况下,这可能不是您想要做的(如果您问我,我认为调用它 setRequestHeader 会是一个更好的选择)。某些代理可能会删除或重写非标准标头的名称,因此您最好坚持通过 URL 参数在 GET URL 中传递数据的约定。

在 BlackBerry 上执行此操作的最佳方法是使用 URLEncodedPostData 类对 URL 参数进行正确编码:

URLEncodedPostData data = new URLEncodedPostData("UTF-8", false);
data.append("method", "session.getToken");
data.append("developerKey", "value");
data.append("clientID", "value");
url = url + "?" + data.toString();

Calling Connection.setRequestProperty() will set the request header, which probably isn't what you want to do in this case (if you ask me I think calling it setRequestHeader would have been a better choice). Some proxies may strip off or rewrite the name of non-standard headers, so you're better off sticking to the convention of passing data in the GET URL via URL parameters.

The best way to do this on a BlackBerry is to use the URLEncodedPostData class to properly encode your URL parameters:

URLEncodedPostData data = new URLEncodedPostData("UTF-8", false);
data.append("method", "session.getToken");
data.append("developerKey", "value");
data.append("clientID", "value");
url = url + "?" + data.toString();
单身狗的梦 2024-08-20 15:50:31

HTTP GET 将数据参数作为 URL 中编码的键/值对发送,就像:

GET /example.html                      // without parameters
GET /example.html?Id=         1        // with one basic parameter
GET /example.html?Id=1&Name=John%20Doo // with two parameters, second encoded

注意遵循字符分隔符规则:

? - split URL in two pieces: adddress to left and paremeters to right
& - must be used to separate on parameter from another

您必须了解特定于平台的本机字符串编码函数。 Javascript 使用 escape,C# 使用 HttpUtility.UrlEncode

HTTP GET send data parameters as key/value pairs encoded within URL, just like:

GET /example.html                      // without parameters
GET /example.html?Id=         1        // with one basic parameter
GET /example.html?Id=1&Name=John%20Doo // with two parameters, second encoded

Note follow rules for character separators:

? - split URL in two pieces: adddress to left and paremeters to right
& - must be used to separate on parameter from another

You must know your platform specific native string encode function. Javascript uses escape, C# uses HttpUtility.UrlEncode

荒岛晴空 2024-08-20 15:50:31

是的,标头和属性几乎就是您可以在 GET 中发送的所有内容。另外,您只能使用一定数量的字符,这取决于浏览器 - 我记得通常大约是 1024 或 2000 个。

Yep, headers and properties are pretty much all you can send in a GET. Also, you're limited to a certain number of characters, which is browser dependent - I seem to recall about 1024 or 2000, typically.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文