parent
010f50dc7d
commit
9bd58e0600
@ -0,0 +1,20 @@
|
||||
package com.platform.http.gfs;
|
||||
|
||||
public class BaseForm1 {
|
||||
|
||||
private String id;
|
||||
private String name;
|
||||
public String getId() {
|
||||
return id;
|
||||
}
|
||||
public void setId(String id) {
|
||||
this.id = id;
|
||||
}
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,7 @@
|
||||
package com.platform.http.gfs;
|
||||
|
||||
public class HttpClientConstant {
|
||||
|
||||
public static String URL_IP_PORT = "http://localhost";
|
||||
|
||||
}
|
@ -0,0 +1,88 @@
|
||||
package com.platform.http.gfs;
|
||||
|
||||
import java.beans.IntrospectionException;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import javax.sound.midi.SysexMessage;
|
||||
|
||||
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.entities.FolderNode;
|
||||
import com.platform.utils.Bean2MapUtils;
|
||||
|
||||
public class HttpUtils {
|
||||
|
||||
public Object sendPost(String subUrl, Object data) {
|
||||
Map<String, Object> resultMap = null;
|
||||
String resultStr = null;
|
||||
HttpClient client = new DefaultHttpClient();
|
||||
HttpPost post = new HttpPost(HttpClientConstant.URL_IP_PORT + subUrl);
|
||||
try {
|
||||
// 传参
|
||||
List<NameValuePair> parameters = new ArrayList<NameValuePair>();
|
||||
// 转 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");
|
||||
Object rest = ht.sendPost("/hello/gain", bas);
|
||||
if (null != rest) {
|
||||
System.out.println(rest);
|
||||
JSONObject jsondata = JSONObject.fromObject(rest);
|
||||
FolderNode fo = (FolderNode) JSONObject.toBean((JSONObject) jsondata, FolderNode.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());
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in new issue