PostMethod和GetMethod用法


注:新浪短接口參考地址:https://www.douban.com/note/249723561/      將長的url鏈接轉換成短鏈接

一、GetMethod

try {
						
	HttpClient client = new HttpClient();
	String OrderId_url ="http://api.t.sina.com.cn/short_url/shorten.json?source=3271760578&url_long="+req.getScheme()+"://"+ req.getServerName()+req.getContextPath()+"/wx/showWxOrderDetail.do?orderId="+thirdPayLogDetail.getSaleOrderSysId();
	GetMethod get = new GetMethod(OrderId_url);						        
     get.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, "UTF-8");
	int status = client.executeMethod(get);
	String res = get.getResponseBodyAsString().trim();
	System.out.println("返回回來的數據:"+res+ "  狀態值:" + status);
	//返回回來的數據:[{"url_short":"http://t.cn/RONu69s","url_long":"http://dev7.89t.cn/ebusiness/wx/showWxOrderDetail.do?orderId=12451","type":0}]  狀態值:200						
	JSONArray result=JSONArray.fromObject(res);//轉json對象
	JSONObject getJsonObj = result.getJSONObject(0);
	int type = (int) getJsonObj.get("type");
	String url_short = getJsonObj.getString("url_short");
	System.out.println("返回狀態:"+type+"=======短接口:"+url_short);

 二、PostMethod

		HttpClient client = new HttpClient();
		String methodName="temporaryOrder.do";
		String serverUrl=
		PostMethod post = new PostMethod(serverUrl);
		post.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, "UTF-8");		
		try {
			int status = client.executeMethod(post);
			String response = post.getResponseBodyAsString().trim();
			System.out.println("返回回來的數據:"+response);
			if (logger.isInfoEnabled()) {
				logger.info("返回信息:" + response + "  狀態值:" + status);
			}
			JSONObject result=JSONObject.fromObject(response);//轉json對象
			JSONObject headOb=result.getJSONObject("head");//頭部信息
			JSONObject resultBody=result.getJSONObject("body");
			if (response.contains("\"code\":\"200\"")) {//請求成功
				//TODO 記錄訂單明細號
				if (resultBody.has("orderDetail")) {

				}
				return true;
			}else{
				throw new BusinessException(headOb.getString("msg"));
			}

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM