httppost发送请求和接收请求
现在两个工程,一个工程发送httppost请求,另外一个工程接收请求,没做过这个,发送请求在网上找资料改了一下,但是如何接收请求搞不懂,求助。
下面代码为发送请求:
public void test1() {
//post请求返回结果
DefaultHttpClient httpClient = new DefaultHttpClient();
JSONObject jsonResult = null;
JSONObject jsonParam = new JSONObject();
jsonParam.put("code", "2001001");
jsonParam.put("serviceCode", "1");
String url = "http://localhost:8080/velox-n";
HttpPost method = new HttpPost(url);
try {
if (null != jsonParam) {
//解决中文乱码问题
StringEntity entity = new StringEntity(jsonParam.toString(), "utf-8");
entity.setContentEncoding("UTF-8");
entity.setContentType("application/json");
method.setEntity(entity);
}
HttpResponse result = httpClient.execute(method);
url = URLDecoder.decode(url, "UTF-8");
/**请求发送成功,并得到响应**/
if (result.getStatusLine().getStatusCode() == 200) {
String str = "";
try {
/**读取服务器返回过来的json字符串数据**/
str = EntityUtils.toString(result.getEntity());
/**把json字符串转换成json对象**/
// jsonResult = JSONObject.fromObject(str);
} catch (Exception e) {
}
}
} catch (IOException e) {
}
}