BlackBerry 中的 Http POST
您好,
我正在尝试从我的 BlackBerry 应用程序设置服务器连接。我能够获得有关服务器状态的响应代码。现在我有一些值必须 POST 到服务器,
就像注册页面值(用户名、密码、年龄)必须发送到服务器。
ConnectionFactory connFact = new ConnectionFactory();
ConnectionDescriptor connDesc;
connDesc = connFact.getConnection(url);
if (connDesc != null)
{
HttpConnection httpConn;
httpConn = (HttpConnection)connDesc.getConnection();
try
{
final int iResponseCode = httpConn.getResponseCode();
UiApplication.getUiApplication().invokeLater(new Runnable()
{
public void run()
{
Dialog.alert("Response code: " + Integer.toString(iResponseCode));
}
});
}
catch (IOException e)
{
System.err.println("Caught IOException: " + e.getMessage());
}
}
这就是我用来获取响应代码的代码。 如果有人能帮助我如何向服务器发出 POST 请求,我将不胜感激。 状态的服务器网址是company.com/app/version/stats,
而注册的服务器网址是 company.com/app/register
谢谢
Greetings,
I am trying to setup a server connection from my BlackBerry Application . I was able to get a response code on the status of the server. Now i have a few values which i have to POST to the server
Its like a registration page values(username, password, age ) have to be sent to the server .
ConnectionFactory connFact = new ConnectionFactory();
ConnectionDescriptor connDesc;
connDesc = connFact.getConnection(url);
if (connDesc != null)
{
HttpConnection httpConn;
httpConn = (HttpConnection)connDesc.getConnection();
try
{
final int iResponseCode = httpConn.getResponseCode();
UiApplication.getUiApplication().invokeLater(new Runnable()
{
public void run()
{
Dialog.alert("Response code: " + Integer.toString(iResponseCode));
}
});
}
catch (IOException e)
{
System.err.println("Caught IOException: " + e.getMessage());
}
}
Thats the code i used to get the response code.
I would appreciate it if someone could help me how i can make a POST request to the server..
the server url for status was company.com/app/version/stats
when it for register it would be
company.com/app/register
Thank you
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您使用什么类型的 POST?如果您只是传递键值对,那么它应该是“application/x-www-form-urlencoded”内容类型的 POST。
所以,你缺少的代码是:
1)。在您的连接上设置正确的内容类型:
2)。准备通过POST发送到服务器的内容:
3)。设置连接的内容长度(此步骤可能是可选的 - 首先尝试不使用此步骤,可能 BB 操作系统足够智能,可以自动设置此步骤):
4)。打开一个OutputStream并将内容写入其中(代码已简化):
What type of a POST do you use? If you are just passing key-value pairs, then it should be a POST of a "application/x-www-form-urlencoded" content-type.
So, what lacks youe code is:
1). Set a proper content-type on your connection:
2). Prepare the content to be sent to the server via the POST:
3). Set content-length for the connection (this step may be optional - try without this first, probably the BB OS is smart enough to set this automatically):
4). Open an OutputStream and write the content to it (the code is simplified):