HttpClientUtil通过xml和form表单方式请求

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;
    }
    
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

wuyongde0922

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值