java swing通过httpclient向服务器端发送post请求如何做
答案:1 悬赏:30 手机版
解决时间 2021-02-08 14:30
- 提问者网友:人生佛魔见
- 2021-02-08 06:20
java swing通过httpclient向服务器端发送post请求如何做
最佳答案
- 五星知识达人网友:鱼芗
- 2021-02-08 06:36
public static String doPostWithBean(String url,Object bean,String...params) throws Exception {
System.err.println(params.length);
HttpClient client = getHttpClient();
HttpPost httppost = new HttpPost(url);
MultipartEntity entity = new MultipartEntity();
for(Field f : bean.getClass().getDeclaredFields()){
f.setAccessible(true);
if(f.get(bean)!=null&&!"".equals(f.get(bean).toString())){
entity.addPart(f.getName(),new StringBody(f.get(bean).toString(),Charset.forName("UTF-8")));
}
}
for(Field f : bean.getClass().getSuperclass().getDeclaredFields()){
f.setAccessible(true);
if(f.get(bean)!=null&&!"".equals(f.get(bean).toString())){
entity.addPart(f.getName(),new StringBody(f.get(bean).toString(),Charset.forName("UTF-8")));
}
}
if(params!=null && params.length!=0) {
Map
for(String paramName:paramsMap.keySet()){
entity.addPart(paramName,new StringBody((String) paramsMap.get(paramName),Charset.forName("UTF-8")));
}
}
httppost.setEntity(entity);
String resp = null;
try {
HttpResponse response = client.execute(httppost);
HttpEntity resEntity = response.getEntity();
if (resEntity != null) {
resp = EntityUtils.toString(resEntity, "UTF-8");
}
if (resEntity != null) {
EntityUtils.consume(resEntity);
}
} finally {
client.getConnectionManager().shutdown();
}
return resp;
}
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯