[英]httpPost request from Android to java server (jetty): parameters get lost?
I am sending off a rather simple httpPost request, following this howto .我正在按照这个 howto发送一个相当简单的 httpPost 请求。 (It's in German, but you can have a look at the HTTP POST example). (它是德语,但您可以查看 HTTP POST 示例)。 This is what I got:这就是我得到的:
HttpPost httpPost = new HttpPost(params[0]);
HttpParams httpParams = new BasicHttpParams();
httpParams.setParameter("title", "message");
//... setting some other parameters like http timeout, which I checked and which work
httpPost.setParams(httpParams);
//HttpEntity myEntity = new StringEntity(messageBody);
//httpPost.setEntity(myEntity);
response = httpClient.execute(httpPost);
(the commented part is something I tried as well, but with no results). (评论部分也是我尝试过的,但没有结果)。
The server code looks like this:服务器代码如下所示:
public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
{
response.setContentType("text/plain;charset=utf-8");
if (target.contentEquals("/postKdm"))
{
String title = request.getParameter("title");
InputStream instream = request.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(instream));
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null)
{
sb.append(line);
}
System.out.println(title);
response.setStatus(HttpServletResponse.SC_OK);
}
}
Where both the String title and the InputStream are null/empty.其中 String 标题和 InputStream 均为空/空。 I've debugged and checked the request object, but couldn't find anything looking like my parameter.我已经调试并检查了请求 object,但找不到任何看起来像我的参数的东西。
Also, I found something sounding similar to my problem here , but the answer didn't help me much as I don't work with Apache Camel and therefore can't use the Exchange class.另外,我在这里发现了一些听起来与我的问题相似的东西,但答案对我没有多大帮助,因为我不使用 Apache Camel,因此不能使用 Exchange class。
Oh, and the similar GET request is working excellent, but here I just got stuck.哦,类似的 GET 请求运行良好,但在这里我卡住了。 :/ :/
I would appreciate any kind of help!我将不胜感激任何帮助!
Kind regards, jellyfish亲切的问候,水母
I still don't know why the "setParams" doesn't work.我仍然不知道为什么“setParams”不起作用。 But I used Wireshark to check my outgoing request, and after that I found a solution using HttpEntity (just as in the commented part above):但是我使用 Wireshark 来检查我的传出请求,之后我找到了使用 HttpEntity 的解决方案(就像上面评论的部分一样):
HttpEntity myEntity = new StringEntity(message);
httpPost.setEntity(myEntity);
response = httpClient.execute(httpPost);
The server side I found out thanks to Chris' answer , only that I, of course, replaced the byte buffer with a char buffer, like here :由于克里斯的回答,我发现了服务器端,只是我当然用字符缓冲区替换了字节缓冲区,如下所示:
private String getInputString() throws IOException
{
InputStream is = request.getInputStream();
if (is != null)
{
Writer writer = new StringWriter();
char[] buffer = new char[request.getContentLength()];
try
{
Reader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
int n;
while ((n = reader.read(buffer)) != -1)
{
writer.write(buffer, 0, n);
}
}
finally
{
is.close();
}
return writer.toString();
}
else
{
return "";
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.