标签:
package com.thinkgem.jeesite.common.utils; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.NameValuePair; import org.apache.http.ParseException; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.HttpClient; import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpPost; import org.apache.http.conn.scheme.Scheme; import org.apache.http.conn.ssl.SSLSocketFactory; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.message.BasicNameValuePair; import org.apache.http.util.EntityUtils; import org.codehaus.jettison.json.JSONObject; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.UnsupportedEncodingException; import java.net.URLEncoder; import java.security.*; import java.security.cert.CertificateException; import java.util.*; public class HttpUtil { HttpClient httpclient=new DefaultHttpClient(); /** * 发送 post请求访问本地应用并根据传递参数不同返回不同结果 */ public String post(String url,String reqEncoding,String respEncoding,JSONObject jsonObj) { String resStr = ""; // 创建httppost HttpPost httppost = new HttpPost( url); // 创建参数队列 //List<NameValuePair> formparams = jsonObj; UrlEncodedFormEntity uefEntity; try { StringEntity sEntity = new StringEntity(jsonObj.toString(),"utf-8");//解决中文乱码问题 sEntity.setContentEncoding("UTF-8"); sEntity.setContentType("application/json"); //httppost.setEntity(uefEntity); httppost.setEntity(sEntity); HttpResponse response; response = httpclient.execute(httppost); HttpEntity entity = response.getEntity(); if (entity != null) { resStr = EntityUtils.toString(entity,respEncoding); } } catch (ClientProtocolException e) { e.printStackTrace(); } catch (UnsupportedEncodingException e1) { e1.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { // 关闭连接,释放资源 // httpclient.getConnectionManager().shutdown(); } return resStr; } /** * 发送 get请求 */ public String get(String url) { String resStr = ""; try { // 创建httpget. HttpGet httpget = new HttpGet(url); // 执行get请求. HttpResponse response = httpclient.execute(httpget); // 获取响应实体 HttpEntity entity = response.getEntity(); // 打印响应状态 System.out.println(response.getStatusLine()); if (entity != null) { resStr=EntityUtils.toString(entity); } } catch (ClientProtocolException e) { e.printStackTrace(); } catch (ParseException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { // 关闭连接,释放资源 //httpclient.getConnectionManager().shutdown(); } return resStr; } public static void main(String[] args) throws ClientProtocolException, IOException { /*HttpUtil ht = new HttpUtil();// 构造参数 String requestTime = DateUtils.getDate("yyyy-MM-dd HH:mm:ss"); requestTime = requestTime .replaceFirst("" + requestTime.charAt(10), "T");//替换空为T requestTime += ".000";//增加毫秒数 ApiAuthUtil apiAuthUtil = new ApiAuthUtil("InterFlight", "Query", "FzSearch", "4874257a-c262-4586-bd4b-09b95a500857", "47dc925ec0a2ec96", requestTime, 96870); String apiURL = apiAuthUtil .getApiURL("http://tcopenapi.17usoft.com/flight/Inter/Query/FzSearch"); System.out.println(apiURL); String result = ht.post(apiAuthUtil.getApiURL("http://tcopenapi.17usoft.com/flight/Inter/Query/FzSearch"), "utf-8"); System.out.println("result:" + result);*/ } }
标签:
原文地址:http://www.cnblogs.com/SHMILYHP/p/5122032.html