请求
package test2;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;
import org.junit.Test;
import net.sf.json.JSONArray;
public class doTest {
@Test
public void test1(){
HttpRequest req = new HttpRequest();
req.setCompany("yunda");
req.setFrom("上海市浦东新区");
req.setTo("广东市深圳南山区");
req.setNumber("12345678");
Parameters pt = new Parameters();
pt.setCallbackurl("http://xxxx/manage/order/sys/rollbackKuaiDi100");
pt.setDepartureCom("yunda");
req.setParameters(pt);
req.setKey("xxxx");
HashMap<String, String> p = new HashMap<String, String>();
p.put("schema", "json");
p.put("param", JsonUtils.objectToJson(req));
try {
String resp = postData("http://poll.kuaidi100.com/poll", p, "utf-8").toString();
System.out.println(resp);
JSONArray json = JSONArray.fromObject(resp);
System.out.println(json);
List<HttpResponse> httpResponse= (List<HttpResponse>)JSONArray.toCollection(json, HttpResponse.class);
if(httpResponse.get(0).isResult()==true){
System.out.println("订阅成功");
}else{
System.out.println("订阅失败");
}
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 发送请求
* @param url
* @param params
* @param codePage
* @return
* @throws Exception
*/
public String postData(String url, Map<String, String> params, String codePage) throws Exception {
final HttpClient httpClient = new HttpClient();
httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(25 * 1000);
httpClient.getHttpConnectionManager().getParams().setSoTimeout(30 * 1000);
final PostMethod method = new PostMethod(url);
if (params != null) {
method.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, codePage);
method.setRequestBody(assembleRequestParams(params));
}
String result = "";
try {
httpClient.executeMethod(method);
result = new String(method.getResponseBody(), codePage);
} catch (final Exception e) {
throw e;
} finally {
method.releaseConnection();
}
return result;
}
/**
* 组装http请求参数
*
* @param params
* @param menthod
* @return
*/
private synchronized static NameValuePair[] assembleRequestParams(Map<String, String> data) {
final List<NameValuePair> nameValueList = new ArrayList<NameValuePair>();
Iterator<Map.Entry<String, String>> it = data.entrySet().iterator();
while (it.hasNext()) {
Map.Entry<String, String> entry = (Map.Entry<String, String>) it.next();
nameValueList.add(new NameValuePair((String) entry.getKey(), (String) entry.getValue()));
}
return nameValueList.toArray(new NameValuePair[nameValueList.size()]);
}
}
回调
package test3
import java.io.IOException
import java.util.HashMap
import java.util.Map
import javax.servlet.ServletException
import javax.servlet.http.HttpServletRequest
import javax.servlet.http.HttpServletResponse
import net.sf.json.JSONObject
public class test{
@RequestMapping("/rollbackKuaiDi100")
@ResponseBody
public Object rollbackKuaiDi100(HttpServletRequest request,HttpServletResponse response){
Map<String,Object> map = new HashMap<String, Object>()
map.put("result",false)
map.put("returnCode","500")
map.put("message","保存失败")
try {
String param = request.getParameter("param")
JSONObject jsonObject = JSONObject.fromObject(param)
System.out.println(param)
// OrderExpress orderExpress = orderExpressService.selectOrderExpressByOrderCode(jsonObject.getString("nu"))
// orderExpress.setStatus(jsonObject.getString("status"))
// orderExpress.setMessage(jsonObject.getString("message"))
// orderExpress.setData(jsonObject.getJSONObject("data"))
// JSONObject jsonResult =(JSONObject) jsonObject.get("lastResult")
// orderExpress.setState(Integer.parseInt(jsonResult.getString("state")))
// orderExpress.setExpressCode(jsonObject.getString("nu"))
// orderExpressService.saveOrUpdateOrderExpress(orderExpress)
// 处理快递结果
map.put("result",true)
map.put("returnCode","200")
map.put("message","保存成功")
//这里必须返回,否则认为失败,过30分钟又会重复推送。
response.getWriter().print(JSONObject.fromObject(map))
} catch (Exception e) {
map.put("message","保存失败" + e.getMessage())
//保存失败,服务端等30分钟会重复推送。
try {
response.getWriter().print(JSONObject.fromObject(map))
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace()
}
}
return "success"
}
}