码迷,mamicode.com
首页 > Web开发 > 详细

httpclient post请求实例(自己写的)

时间:2016-09-23 16:32:54      阅读:157      评论:0      收藏:0      [点我收藏+]

标签:

package com.gop.gplus.trade.common.utils;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
* Created by zhangtan on 2016/9/23.
*/
public class HttpClientUtil {
public static String post(Map<String, Object> sParaTemp, String url) throws Exception{
HttpClient httpClient = new HttpClient();

PostMethod post = new PostMethod(url);
post.setRequestHeader("Content-Type","application/x-www-form-urlencoded;charset=utf-8");

List<String> keys = new ArrayList<String>(sParaTemp.keySet());
NameValuePair[] param = new NameValuePair[keys.size()+1];
for (int i = 0; i < keys.size(); i++) {
String name = keys.get(i);
Object object = sParaTemp.get(name);
String value = "";
if (object != null) {
value = (String) sParaTemp.get(name);
}
//添加参数
param[i] = new NameValuePair(name, value);
post.setParameter(param[i].getName(),param[i].getValue());
//System.out.println(param[i].getName());
}
HttpMethod method = post;
httpClient.executeMethod(method);
String response = method.getResponseBodyAsString();
post.releaseConnection();
return response;
}
}

httpclient post请求实例(自己写的)

标签:

原文地址:http://www.cnblogs.com/zhangtan/p/5900392.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!