You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
aggregation-platform/src/com/platform/http/gfs/HttpUtils.java

86 lines
2.6 KiB

package com.platform.http.gfs;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import net.sf.json.JSONObject;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import com.platform.utils.Bean2MapUtils;
public class HttpUtils {
public String sendPost(String subUrl, Object data) {
String resultStr = null;
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(HttpClientConstant.URL_IP_PORT + subUrl);
try {
// 传参
List<NameValuePair> parameters = new ArrayList<NameValuePair>();
if (null != data) {
// 转 map
Map<String, Object> map = Bean2MapUtils.convertBean(data);
// 转json
JSONObject jsondata = JSONObject.fromObject(map);
// 封装数据
parameters.add(new BasicNameValuePair("jsondata", jsondata
.toString()));
parameters.add(new BasicNameValuePair("type", data.getClass()
.getName()));
// 传参 放入 url的 form中
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(parameters,
"UTF-8");
post.setEntity(entity);
}
// 发送
HttpResponse respone = client.execute(post);
HttpEntity result = respone.getEntity();
if (null != result) {
resultStr = EntityUtils.toString(result);
}
// resultMap = Bean2MapUtils.convertBean(result);
} catch (Exception e) {
System.out.println(e);
}
return resultStr;
}
// public static void main(String[] args) {
// Date d = new Date();
// HttpUtils ht = new HttpUtils();
// BaseForm1 bas = new BaseForm1();
// bas.setId("12");
// bas.setName("n12");
// String rest = ht.sendPost("gfs/getAllvolume", null);
// if (null != rest) {
// System.out.println(rest);
// JSONArray jsondata = JSONArray.fromObject(rest);
// ArrayList<VolumeEntity> fo = (ArrayList<VolumeEntity>) JSONArray.toCollection(jsondata, VolumeEntity.class);
// System.err.println(fo);
// try {
// System.err.println(Bean2MapUtils.convertBean(fo));
// } catch (IllegalAccessException | InvocationTargetException
// | IntrospectionException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
// }
//
// Date r = new Date();
// System.err.println(r.getTime() - d.getTime());
// }
}