如何在 BlackBerry 上创建 HTTP PUT 请求

发布于 2024-11-19 12:08:54 字数 696 浏览 6 评论 0原文

提供一些背景知识: 我正在构建一个 BlackBerry 前端来与后端 Web 服务通信,该服务仅支持对许多重要资源的 PUT 请求,而不是 POST 请求。

这个后端在 iOS 上工作得很好,但我似乎无法在 BlackBerry 上创建 PUT 请求。查看 BlackBerry API,我已经能够使用以下代码创建 GET/POST

HttpConnection conn = (HttpConnection) Connector.open(URL);  
conn.setRequestMethod(HttpConnection.GET);

请求

conn.setRequestMethod(HttpConnection.POST);

: blackberry.com/developers/docs/5.0.0api/javax/microedition/io/HttpConnection.html#setRequestMethod%28java.lang.String%29" rel="nofollow">此处。我尝试手动设置 conn.setRequestMethod("PUT"); 但我不断收到来自服务器的 400 错误。

有谁知道如何正确创建 BlackBerry 的 PUT 请求?到目前为止,我在整个网络上的搜索还没有带回任何可用的解决方案。 =(谢谢!

To give some background:
I'm building a BlackBerry frontend to talk to a backend web service built to support only PUT requests for a lot of important resources instead of POST requests.

This backend worked fine for iOS but I can't seem to create a PUT request on the BlackBerry. Looking through the BlackBerry API, I've been able to create GET/POST requests with the following code:

HttpConnection conn = (HttpConnection) Connector.open(URL);  
conn.setRequestMethod(HttpConnection.GET);

or

conn.setRequestMethod(HttpConnection.POST);

It seems that the HttpConnection class should support a PUT command as seen in the API here. I've tried manually setting conn.setRequestMethod("PUT"); but I keep getting a 400 error from the server.

Does anyone know how to properly create a PUT request for the BlackBerry? My search throughout the web hasn't brought back any usable solutions so far. =( Thanks!

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

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

发布评论

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

评论(1

慢慢从新开始 2024-11-26 12:08:54

HTTP 400 表示“由于语法错误,服务器无法理解该请求”。

那么,如果您使用conn.setRequestMethod(HttpConnection.POST);,您能否确认您的代码工作正常?

这就是我问这个的原因:
根据 RESTful Web 服务模式,区别应该在于 PUT 用于更新服务器上的资源,而 POST 用于在服务器上创建资源。因此,如果您的网络代码作为 POST 工作正常,那么就确认您的代码基本上是正常的。

还有一点是“服务器端使用什么框架/技术?”。这可能很重要,因为浏览器本身不支持 PUT(仅 GET 和 POST),因此 Web 应用程序通常通过在 Web 表单中放置隐藏表单字段(例如“_method=PUT”)来模拟 PUT。因此,当用户单击“提交”时,浏览器会执行一个包含“_method=PUT”参数的 POST,以便服务器可以识别它是一个 PUT。这是在 Rails 中使用的,我怀疑其他人也在做一些类似的事情。如果这是您的情况,那么只需尝试将您的 POST 参数附加到您的 Web 应用程序参数所需的参数(如“_method=PUT”)。

HTTP 400 means "The request could not be understood by the server due to malformed syntax.".

So could you confirm your code works OK if you use conn.setRequestMethod(HttpConnection.POST);?

Here is why I'm asking this:
According to RESTful web services pattern the difference should be in that PUT is used to update a resource on server while POST is used to create a resource on server. So if your networking code works OK as a POST, then it confirms your code is basically OK.

Another point is "what framework/technology is used on server side?". This could be important, because browsers do not support PUTs (only GET and POST) natively, so web-apps usually simulate PUTs by putting a hidden form field (smth like "_method=PUT") in web-forms. So when user clicks Submit, then browser does a POST which includes "_method=PUT" param, so the server can recognize it is a PUT. This is used in Rails and I suspect others do some similar stuff. If this is your case, then just try appending your POST params with a required by your web-app param (smth like "_method=PUT").

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