對這文章發表回應
發表限制: 非會員 可以發表
發表者: 冷日 發表時間: 2019/5/30 14:48:03
HttpClient請求DefaultHttpClient過時替換
package com.huanlv.util;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.ParseException;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import net.sf.json.JSONObject;
/**
*
* @author david
*
*/
@SuppressWarnings("deprecation")
public class HttpClientUtil {
//
public static final String learnSelUrl="http://192.168.0.124:8080/site/trdcredit/jxlxxwactivereq";
/**
* post請求
* @param url
* @param param
* @return
* @throws IOException
*/
public static JSONObject postJsonData(String url,Map<String,String> params){
CloseableHttpClient httpclient = HttpClientUtil.createDefault();
HttpPost httpPost = new HttpPost(url);
//拼接參數
List<NameValuePair> list = new ArrayList<NameValuePair>();
for (Map.Entry<String, String> entry : params.entrySet()) {
String key = entry.getKey().toString();
String value = entry.getValue().toString();
System.out.println("key=" + key + " value=" + value);
NameValuePair pair = new BasicNameValuePair(key, value);
list.add(pair);
}
CloseableHttpResponse response=null;
try {
httpPost.setEntity(new UrlEncodedFormEntity(list));
response = httpclient.execute(httpPost);
} catch (UnsupportedEncodingException e1) {
e1.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
/**請求發送成功,並得到響應**/
JSONObject jsonObject=null;
if(response.getStatusLine().getStatusCode() == HttpStatus.SC_OK){
HttpEntity httpEntity = response.getEntity();
String result=null;
try {
result = EntityUtils.toString(httpEntity);
} catch (ParseException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}// 返回json格式:
jsonObject = JSONObject.fromObject(result);
}
return jsonObject;
}
/**
* Creates {@link CloseableHttpClient} instance with default
* configuration.
*/
public static CloseableHttpClient createDefault() {
return HttpClientBuilder.create().build();
}
/**
* http發送post請求
* @param url
* @param maps
* @return
*/
@SuppressWarnings({ "resource" })
public static JSONObject sendPost(String url,Map<String,String> params){
DefaultHttpClient client = new DefaultHttpClient();
/**NameValuePair是傳送給服務器的請求參數 param.get("name") **/
List<NameValuePair> list = new ArrayList<NameValuePair>();
for (Map.Entry<String, String> entry : params.entrySet()) {
String key = entry.getKey().toString();
String value = entry.getValue().toString();
System.out.println("key=" + key + " value=" + value);
NameValuePair pair = new BasicNameValuePair(key, value);
list.add(pair);
}
UrlEncodedFormEntity entity=null;
try {
/**設置編碼 **/
entity = new UrlEncodedFormEntity(list,"UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
/**新建一個post請求**/
HttpPost post = new HttpPost(url);
post.setEntity(entity);
HttpResponse response=null;
try {
/**客服端向服務器發送請求**/
response = client.execute(post);
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
/**請求發送成功,並得到響應**/
JSONObject jsonObject=null;
if(response.getStatusLine().getStatusCode() == HttpStatus.SC_OK){
HttpEntity httpEntity = response.getEntity();
String result=null;
try {
result = EntityUtils.toString(httpEntity);
} catch (ParseException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}// 返回json格式:
jsonObject = JSONObject.fromObject(result);
}
return jsonObject;
}
}
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.ParseException;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import net.sf.json.JSONObject;
/**
*
* @author david
*
*/
@SuppressWarnings("deprecation")
public class HttpClientUtil {
//
public static final String learnSelUrl="http://192.168.0.124:8080/site/trdcredit/jxlxxwactivereq";
/**
* post請求
* @param url
* @param param
* @return
* @throws IOException
*/
public static JSONObject postJsonData(String url,Map<String,String> params){
CloseableHttpClient httpclient = HttpClientUtil.createDefault();
HttpPost httpPost = new HttpPost(url);
//拼接參數
List<NameValuePair> list = new ArrayList<NameValuePair>();
for (Map.Entry<String, String> entry : params.entrySet()) {
String key = entry.getKey().toString();
String value = entry.getValue().toString();
System.out.println("key=" + key + " value=" + value);
NameValuePair pair = new BasicNameValuePair(key, value);
list.add(pair);
}
CloseableHttpResponse response=null;
try {
httpPost.setEntity(new UrlEncodedFormEntity(list));
response = httpclient.execute(httpPost);
} catch (UnsupportedEncodingException e1) {
e1.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
/**請求發送成功,並得到響應**/
JSONObject jsonObject=null;
if(response.getStatusLine().getStatusCode() == HttpStatus.SC_OK){
HttpEntity httpEntity = response.getEntity();
String result=null;
try {
result = EntityUtils.toString(httpEntity);
} catch (ParseException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}// 返回json格式:
jsonObject = JSONObject.fromObject(result);
}
return jsonObject;
}
/**
* Creates {@link CloseableHttpClient} instance with default
* configuration.
*/
public static CloseableHttpClient createDefault() {
return HttpClientBuilder.create().build();
}
/**
* http發送post請求
* @param url
* @param maps
* @return
*/
@SuppressWarnings({ "resource" })
public static JSONObject sendPost(String url,Map<String,String> params){
DefaultHttpClient client = new DefaultHttpClient();
/**NameValuePair是傳送給服務器的請求參數 param.get("name") **/
List<NameValuePair> list = new ArrayList<NameValuePair>();
for (Map.Entry<String, String> entry : params.entrySet()) {
String key = entry.getKey().toString();
String value = entry.getValue().toString();
System.out.println("key=" + key + " value=" + value);
NameValuePair pair = new BasicNameValuePair(key, value);
list.add(pair);
}
UrlEncodedFormEntity entity=null;
try {
/**設置編碼 **/
entity = new UrlEncodedFormEntity(list,"UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
/**新建一個post請求**/
HttpPost post = new HttpPost(url);
post.setEntity(entity);
HttpResponse response=null;
try {
/**客服端向服務器發送請求**/
response = client.execute(post);
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
/**請求發送成功,並得到響應**/
JSONObject jsonObject=null;
if(response.getStatusLine().getStatusCode() == HttpStatus.SC_OK){
HttpEntity httpEntity = response.getEntity();
String result=null;
try {
result = EntityUtils.toString(httpEntity);
} catch (ParseException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}// 返回json格式:
jsonObject = JSONObject.fromObject(result);
}
return jsonObject;
}
}
原文出處: HttpClient请求DefaultHttpClient过时替换 - u010849331的专栏 - CSDN博客