HTTP 后编码
我尝试做什么
大家好,
我尝试为我的服务器创建一个登录名,以便我可以通过我的应用程序访问其上的数据。为此,我创建了一个名为 '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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的代码看起来像您正在发送 html 表单。事实上,如果您使用
-d
选项,curl 会向 http 标头添加内容类型(值为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:
您需要更改的是 String 实体部分。您应该进行以下更改;
用户名和密码是在程序上输入的变量。
最后,如果您忘记设置权限,它也将无法工作。您需要设置 android.permission.INTERNET
What you need to change is the String entity part. You should change with the following;
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