服务端
@RestController
@RequestMapping("/landary")
public class landaryController {
@RequestMapping("adduser")
public JSONObject addUser(@RequestBody JSONObject userEntity)
{
System.out.println(JSONObject.toJSONString(userEntity));
JSONObject json=new JSONObject();
json.fluentPut("code","500").fluentPut("result",userEntity);
return json;
}
@RequestMapping("showuser")
public Object showUser()
{
return JSON.toJSONString("hhh");
}
}
客户端post请求
public static String sendSms(String uid,String title,String content){
HttpClient httpclient = new DefaultHttpClient();
String smsUrl="http://127.0.0.1:8088/landary/adduser";
HttpPost httppost = new HttpPost(smsUrl);
String strResult = "";
try {
JSONObject jobj = new JSONObject();
jobj.put("uid", uid);
jobj.put("title", title);
jobj.put("content",content);
System.out.println(jobj.toString());
// nameValuePairs.add(new BasicNameValuePair("msg", (jobj.toString())));
/* httppost.addHeader("Content-type", "application/json; charset=utf-8");
httppost.setHeader("Accept", "application/json");
httppost.setEntity(new StringEntity(jobj.toString(), Charset.forName("UTF-8")));*/
StringEntity s = new StringEntity(jobj.toString());
s.setContentEncoding("UTF-8");
s.setContentType("application/json");//发送json数据需要设置contentType
httppost.setEntity(s);
HttpResponse response = httpclient.execute(httppost);
if (response.getStatusLine().getStatusCode() == 200) {
/*读返回数据*/
String conResult = EntityUtils.toString(response
.getEntity());
System.out.println(conResult);
JSONObject sobj = new JSONObject();
sobj = JSONObject.parseObject(conResult);
String result = sobj.getString("result");
String code = sobj.getString("code");
if(code.equals("500")){
System.out.println(result);
strResult += "发送成功";
}else{
strResult += "发送失败,"+code;
}
} else {
String err = response.getStatusLine().getStatusCode()+"";
strResult += "发送失败:"+err;
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return strResult;
}
get请求
/**
* 发送 get请求
*/
public void get() {
CloseableHttpClient httpclient = HttpClients.createDefault();
try {
// 创建httpget.
HttpGet httpget = new HttpGet("http://127.0.0.1:8088/landary/showuser");
System.out.println("executing request " + httpget.getURI());
// 执行get请求.
CloseableHttpResponse response = httpclient.execute(httpget);
try {
// 获取响应实体
HttpEntity entity = response.getEntity();
System.out.println("--------------------------------------");
// 打印响应状态
System.out.println(response.getStatusLine());
if (entity != null) {
// 打印响应内容长度
System.out.println("Response content length: " + entity.getContentLength());
// 打印响应内容
System.out.println("Response content: " + EntityUtils.toString(entity));
}
System.out.println("------------------------------------");
} finally {
response.close();
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (ParseException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
// 关闭连接,释放资源
try {
httpclient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}