无法从httpRequest获取参数

时间:2012-10-19 09:05:09

标签: java http servlets apache-httpclient-4.x

我想将字符串从客户端传输到Web服务器。

客户代码:

String uriString = "http://128.128.4.120:8080/GCMService/GCMBroadcast";
URI uri = null;
try {
    uri = new URI(uriString);
} catch (URISyntaxException e) {
    e.printStackTrace();
}
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPut httpPut = null;
if(uri!=null)
httpPut = new HttpPut(uri);
HttpParams params = new BasicHttpParams();
params.setParameter("mymsg", "HELLO SERVER");
httpClient.setParams(params);
HttpResponse resp = httpClient.execute(httpPut);

服务器代码:

@Override
protected void doPut(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {      
    System.out.println(req.getParameter("mymsg"));

}

服务器的控制台打印“null”,我希望每次客户端请求httpPut时都应该是“HELLO SERVER”。这是怎么造成的,如何解决呢。

1 个答案:

答案 0 :(得分:0)

String uriString = "http://128.128.4.120:8080/GCMService/GCMBroadcast";
URIBuilder uriBuilder = new URIBuilder(uriString);
uriBuilder.addParameter("mymsg", "HELLO SERVER");
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPut httpPut = new HttpPut(uriBuilder.build());
HttpResponse resp = httpClient.execute(httpPut);