admin管理员组

文章数量:1559111

packagecom.platform.utilmon;importjava.io.ioexception;importjava.io.unsupportedencodingexception;importjava.util.arraylist;importjava.util.list;importjava.util.map;importjava.util.set;importorg.apache.http.httpentity;importorg.apache.http.httpresponse;importorg.apache.http.namevaluepair;importorg.apache.http.parseexception;importorg.apache.http.client.clientprotocolexception;importorg.apache.http.client.entity.urlencodedformentity;importorg.apache.http.client.methods.httpget;importorg.apache.http.client.methods.httppost;importorg.apache.http.client.methods.httpurirequest;importorg.apache.http.impl.client.defaulthttpclient;importorg.apache.http.message.basicnamevaluepair;importorg.apache.http.protocol.http;importorg.apache.http.util.entityutils;importorg.apache.log4j.logger;/***

* ============================================

* 功能描述:发送post/get请求

============================================*/

public classhttpxmlclient {private static logger log = logger.getlogger(httpxmlclient.class);public static string post(string url, mapparams) {

defaulthttpclient httpclient= newdefaulthttpclient();

string body= null;

log.info("create httppost:" url);

httppost post=postform(url, params);

body=invoke(httpclient, post);

httpclient.getconnectionmanager().shutdown();returnbody;

}public staticstring get(string url) {

defaulthttpclient httpclient= newdefaulthttpclient();

string body= null;

log.info("create httppost:" url);

httpget get= newhttpget(url);

body=invoke(httpclient, get);

httpclient.getconnectionmanager().shutdown();returnbody;

}private staticstring invoke(defaulthttpclient httpclient,

httpurirequest httpost) {

httpresponse response=sendrequest(httpclient, httpost);

string body=paseresponse(response);returnbody;

}private staticstring paseresponse(httpresponse response) {

log.info("get response from http server..");

httpentity entity=response.getentity();

log.info("response status: " response.getstatusline());

string charset=entityutils.getcontentcharset(entity);

log.info(charset);

string body= null;try{

body=entityutils.tostring(entity);

log.info(body);

}catch(parseexception e) {

e.printstacktrace();

}catch(ioexception e) {

e.printstacktrace();

}returnbody;

}private statichttpresponse sendrequest(defaulthttpclient httpclient,

httpurirequest httpost) {

log.info("execute post...");

httpresponse response= null;try{

response=httpclient.execute(httpost);

}catch(clientprotocolexception e) {

e.printstacktrace();

}catch(ioexception e) {

e.printstacktrace();

}returnresponse;

}private static httppost postform(string url, mapparams){

httppost httpost= newhttppost(url);

list nvps = new arraylist ();

set keyset =params.keyset();for(string key : keyset) {

nvps.add(newbasicnamevaluepair(key, params.get(key)));

}try{

log.info("set utf-8 form entity to httppost");

httpost.setentity(newurlencodedformentity(nvps, http.utf_8));

}catch(unsupportedencodingexception e) {

e.printstacktrace();

}returnhttpost;

}

}

本文标签: 腾讯企业邮箱邮件企业