HTTP 后编码

发布于 2024-12-27 14:23:43 字数 1788 浏览 0 评论 0原文

我尝试做什么


大家好,

我尝试为我的服务器创建一个登录名,以便我可以通过我的应用程序访问其上的数据。为此,我创建了一个名为 'public void doLogin(final String username, Final String password)' 的方法。在此方法中,我启动一个线程,在其中将用户名和密码发布到我的服务器。现在问题开始了。当我发帖时,我的服务器无法处理我的帖子,如下所示:'"username="+username+"&password="+password'

为了尝试是否是服务器端问题(实际上不是),我使用相同的参数在 'curl -d' 上发了一篇帖子,并且得到了没有任何问题的响应。

供您参考,服务器在 Ruby3 上运行

问题


我需要如何将我的帖子更改为可以处理我发送的数据的服务器。顺便说一下,我发布的字符串必须看起来完全像这样: '"username="+username+"&password="+password //thismeans username=LEUSER&password=LEPASS'

请告诉我我需要改变什么,一些代码片段或教程会很棒。在这里您可以找到 doLogin 方法的重要代码片段

代码


public void doLogin(final String username, final String password) {

        Thread t = new Thread() {
            public void run() {

                String URL = "http://192.168.110.126:3000/sessions.json"; //für momentane testzwecke

                DefaultHttpClient client = new DefaultHttpClient();
                HttpResponse response;

                try {
                    HttpPost post = new HttpPost(URL);
                    StringEntity se = new StringEntity("username="+username+"&password="+password);
                    post.setEntity(se);

                    response = client.execute(post);

                    if (response != null) {

                    //response handling
                        handler.sendEmptyMessage(0);
                    }

                } catch (Exception e) {
                    e.printStackTrace();
                    Log.e("DataHandler", "URLConnection-Error" + e);
                }

            }

        };
        t.start();
    }

请指导我有关此问题。

What I try to do


Hello Guys,

I try to create a login for my server, that I can access the data on it over my app. For this i created a Methode called 'public void doLogin(final String username, final String password)' . In this Methode I start a Thread in which I post the username and password to my server. Now to problem starts. When I do the post my server can't handle my post which looks like this: '"username="+username+"&password="+password'.

To try if its a serverside problem (infact its not) I made a post over 'curl -d' with the same parameters and I get a response without any problems.

For your information the server runs on Ruby3

Question


How do I need to change my Post to the server that it can handle the data I send. By the way the string I post must look exactly like that: '"username="+username+"&password="+password //this means username=LEUSER&password=LEPASS'

Please tell me what I need to change, some code snippets or tutorials would be great. Down here you find the importrent codesnippets of the doLogin Methode

Code


public void doLogin(final String username, final String password) {

        Thread t = new Thread() {
            public void run() {

                String URL = "http://192.168.110.126:3000/sessions.json"; //für momentane testzwecke

                DefaultHttpClient client = new DefaultHttpClient();
                HttpResponse response;

                try {
                    HttpPost post = new HttpPost(URL);
                    StringEntity se = new StringEntity("username="+username+"&password="+password);
                    post.setEntity(se);

                    response = client.execute(post);

                    if (response != null) {

                    //response handling
                        handler.sendEmptyMessage(0);
                    }

                } catch (Exception e) {
                    e.printStackTrace();
                    Log.e("DataHandler", "URLConnection-Error" + e);
                }

            }

        };
        t.start();
    }

Please Guide me Regarding This.

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

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

发布评论

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

评论(2

送君千里 2025-01-03 14:23:43

您的代码看起来像您正在发送 html 表单。事实上,如果您使用 -d 选项,curl 会向 http 标头添加内容类型(值为 application/x-www-form-urlencoded)。

所以尝试像这样添加标题字段:

HttpPost post = new HttpPost(URL);
post.addHeader("Content-Type","application/x-www-form-urlencoded");
...

Your code looks like you are sending a html form. In fact curl adds a content-type (with value application/x-www-form-urlencoded) to the http header if you use the -d option.

So try to add the header field like this:

HttpPost post = new HttpPost(URL);
post.addHeader("Content-Type","application/x-www-form-urlencoded");
...
╰◇生如夏花灿烂 2025-01-03 14:23:43

您需要更改的是 String 实体部分。您应该进行以下更改;

ArrayList<NameValuePair> requestParameters = new ArrayList<NameValuePair>();    
requestParameters.add(new BasicNameValuePair("username", username));
requestParameters.add(new BasicNameValuePair("password", password));

用户名和密码是在程序上输入的变量。

最后,如果您忘记设置权限,它也将无法工作。您需要设置 android.permission.INTERNET

What you need to change is the String entity part. You should change with the following;

ArrayList<NameValuePair> requestParameters = new ArrayList<NameValuePair>();    
requestParameters.add(new BasicNameValuePair("username", username));
requestParameters.add(new BasicNameValuePair("password", password));

username & password are the variables that are input on the program.

Lastly, if you forget to set permission it also won't work. You need to set android.permission.INTERNET

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