例子用到spring+springMvc框架
httpclient例子:
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.CoreConnectionPNames;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import net.sf.json.JSONObject;
@Controller
@RequestMapping("/httpclient")
public class OrderController {
@RequestMapping(value = "/test", method = RequestMethod.POST)
@ResponseBody
public JSONObject willArrive(@RequestParam Map<String, Object> param, String url)
throws ClientProtocolException, IOException {
url = "http://localhost:9200/server/test";// 这个地址访问到服务器
Map<String, Object> map = null;// 传送的内容集
boolean isSuccess = false;// httpclient请求是否成功
map.put("message", "这是httpclient的信息");// 添加传送的内容
HttpPost post = new HttpPost(url);
HttpClient httpClient = new DefaultHttpClient();
httpClient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 2000);// 设置超时时间
httpClient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, 2000);
post.setHeader("Content-type", "application/json; charset=utf-8");// 构造消息头
/* post.setHeader("Connection", "Close"); */
String sessionId = getSessionId();
post.setHeader("SessionId", sessionId);
StringEntity entity = new StringEntity(map.toString(), Charset.forName("UTF-8"));// 构造消息实体
entity.setContentEncoding("UTF-8");
entity.setContentType("application/json");// 发送Json格式的数据请求
post.setEntity(entity);// 添加消息实体
HttpResponse response = httpClient.execute(post);// 执行post访问
HttpEntity httpEntity = response.getEntity();// 返回数据
String returnEntity = EntityUtils.toString(httpEntity,"utf-8");
System.out.println(returnEntity);
/*获取返回的json数据
JSONObject json = JSONObject.fromObject(returnEntity);
String resultCode = json.getString("resultCode");
String errorMsg = json.getString("errorMsg");
System.out.println("==============="+errorMsg);*/
int statusCode = response.getStatusLine().getStatusCode();// 检验返回码
if (statusCode != HttpStatus.SC_OK) {// 返回成功
System.out.println("请求出错: " + statusCode);
isSuccess = false;
} else {
isSuccess = true;
}
if (post != null) {// 释放连接
post.releaseConnection();
}
JSONObject sendResult = new JSONObject();
sendResult.put("sendResult", isSuccess);
return sendResult;
}
public static String getSessionId() {// 构建唯一回话ID
UUID uuid = UUID.randomUUID();
String str = uuid.toString();
return str.substring(0, 8) + str.substring(9, 13) + str.substring(14, 18) + str.substring(19, 23)
+ str.substring(24);
}
}
对应的服务器例子:
import java.util.Map;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import net.sf.json.JSONObject;
@RestController
@RequestMapping(value = "/server")
public class TestController {
@RequestMapping(value = "test")
public JSONObject orderTest(@RequestParam Map<String, Object> parameter) {
String msg = String.valueOf(parameter.get("message"));
System.out.println("message:"+msg);
System.out.println("这里是模拟服务器的orderTest()方法");
JSONObject json = new JSONObject();
json.put("resultCode", "1");
json.put("errorMsg", "there are no errorMsg");
return json;// 返回一个json数据
}
}
写好发布后,访问 localhost:9200/httpclient/test (注意自己的端口)可以访问到client,在client里自动访问到url对应的服务器,实现服务器的逻辑并返回数据到client。