package util;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import org.apache.http.Consts;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
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.springframework.util.CollectionUtils;
public class HttpUtil {
/**
* 以请求体方式提交请求
* @param url
* @param xml
* @return
*/
public static String postUrl(String url,String xml){
DefaultHttpClient client = new DefaultHttpClient();
HttpPost httpost = new HttpPost(url);
try {
httpost.setEntity(new StringEntity(xml,"UTF-8"));
HttpResponse response = client.execute(httpost);
//HttpEntity httpEntity = response.getEntity();
String result = EntityUtils.toString(response.getEntity(),"UTF-8");
return result;
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
/**
*以form表单方式提交请求
* @param url
* @param params
*/
public static final String postParamUrl(String url,Map<String,String> params){
DefaultHttpClient client = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);
List<NameValuePair> formparams = new ArrayList<NameValuePair>();
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(formparams, Consts.UTF_8);
httppost.setEntity(entity);
System.out.println(httppost.getRequestLine());
if(!CollectionUtils.isEmpty(params)){
for(Iterator<Entry<String, String>> it = params.entrySet().iterator();it.hasNext();){
Entry<String, String> entry = it.next();
String key = entry.getKey();
String value = entry.getValue();
formparams.add(new BasicNameValuePair(key,value));
}
}
try {
HttpResponse response = client.execute(httppost);
String result = EntityUtils.toString(response.getEntity(),Consts.UTF_8);
return result;
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import org.apache.http.Consts;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
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.springframework.util.CollectionUtils;
public class HttpUtil {
/**
* 以请求体方式提交请求
* @param url
* @param xml
* @return
*/
public static String postUrl(String url,String xml){
DefaultHttpClient client = new DefaultHttpClient();
HttpPost httpost = new HttpPost(url);
try {
httpost.setEntity(new StringEntity(xml,"UTF-8"));
HttpResponse response = client.execute(httpost);
//HttpEntity httpEntity = response.getEntity();
String result = EntityUtils.toString(response.getEntity(),"UTF-8");
return result;
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
/**
*以form表单方式提交请求
* @param url
* @param params
*/
public static final String postParamUrl(String url,Map<String,String> params){
DefaultHttpClient client = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);
List<NameValuePair> formparams = new ArrayList<NameValuePair>();
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(formparams, Consts.UTF_8);
httppost.setEntity(entity);
System.out.println(httppost.getRequestLine());
if(!CollectionUtils.isEmpty(params)){
for(Iterator<Entry<String, String>> it = params.entrySet().iterator();it.hasNext();){
Entry<String, String> entry = it.next();
String key = entry.getKey();
String value = entry.getValue();
formparams.add(new BasicNameValuePair(key,value));
}
}
try {
HttpResponse response = client.execute(httppost);
String result = EntityUtils.toString(response.getEntity(),Consts.UTF_8);
return result;
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}