我想将字符串从客户端传输到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"));
}
每次客户端请求httpPut时,服务器的控制台都会显示“ null”,我希望它应该是“ HELLO SERVER”。这是怎么引起的,我该如何解决。
最佳答案
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);