java 表单xml方式提交,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 params){         DefaultHttpClient client = new DefaultHttpClient();         HttpPost httppost = new HttpPost(url);         List formparams = new ArrayList();         UrlEncodedFormEntity entity = new UrlEncodedFormEntity(formparams, Consts.UTF_8);         httppost.setEntity(entity);         System.out.println(httppost.getRequestLine());         if(!CollectionUtils.isEmpty(params)){             for(Iterator> it = params.entrySet().iterator();it.hasNext();){                 Entry 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;     }      }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值