BlackBerry 中的 Http POST

发布于 2024-11-10 18:09:34 字数 1146 浏览 6 评论 0原文

您好,

我正在尝试从我的 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 技术交流群。

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

发布评论

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

评论(2

大海や 2024-11-17 18:09:34

您使用什么类型的 POST?如果您只是传递键值对,那么它应该是“application/x-www-form-urlencoded”内容类型的 POST。

所以,你缺少的代码是:

1)。在您的连接上设置正确的内容类型:

httpConn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");

2)。准备通过POST发送到服务器的内容:

URLEncodedPostData encPostData = new URLEncodedPostData("UTF-8", false);
encPostData.append("username", username);
encPostData.append("password", password);
encPostData.append("age", age);
byte[] postData = encPostData.toString().getBytes("UTF-8");

3)。设置连接的内容长度(此步骤可能是可选的 - 首先尝试不使用此步骤,可能 BB 操作系统足够智能,可以自动设置此步骤):

httpConn.setRequestProperty("Content-Length", String.valueOf(postData.length));

4)。打开一个OutputStream并将内容写入其中(代码已简化):

OutputStream os = httpConn.openOutputStream();
os.write(postData);
os.flush();

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:

httpConn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");

2). Prepare the content to be sent to the server via the POST:

URLEncodedPostData encPostData = new URLEncodedPostData("UTF-8", false);
encPostData.append("username", username);
encPostData.append("password", password);
encPostData.append("age", age);
byte[] postData = encPostData.toString().getBytes("UTF-8");

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):

httpConn.setRequestProperty("Content-Length", String.valueOf(postData.length));

4). Open an OutputStream and write the content to it (the code is simplified):

OutputStream os = httpConn.openOutputStream();
os.write(postData);
os.flush();
回忆躺在深渊里 2024-11-17 18:09:34
 ...
httpConn = (HttpConnection)connDesc.getConnection();    
httpConn.setRequestMethod(HttpConnection.POST);
httpConn.setRequestProperty("username",name);
httpConn.setRequestProperty("password",pass);
....
 ...
httpConn = (HttpConnection)connDesc.getConnection();    
httpConn.setRequestMethod(HttpConnection.POST);
httpConn.setRequestProperty("username",name);
httpConn.setRequestProperty("password",pass);
....
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文