import org.apache.http.*;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.*;
public class HttpClientUtil {
private static HttpClient client = null;
private static Logger logger = LoggerFactory.getLogger(HttpClientUtil.class);
private HttpClientUtil(){}
static{
if(null == client) {
client = HttpClientFactory.createHttpClient();
}
}
public static String doGetRequest(String urlstr) {
String response = "";
HttpGet httpGet = new HttpGet(urlstr);
try {
HttpResponse httpResponse = client.execute(httpGet);
HttpEntity httpEntity = httpResponse.getEntity();
response = EntityUtils.toString(httpEntity);
} catch (Exception e) {
logger.error("错误信息" + e.getMessage());
e.printStackTrace();
} finally {
httpGet.releaseConnection();
}
return response.trim();
}
/**
*
* @param _InputStream
* @param Charset
* @return
*/
public String GetResponseString(InputStream _InputStream, String Charset) {
String response = "";
try {
if (_InputStream != null) {
StringBuffer buffer = new StringBuffer();
InputStreamReader isr = new InputStreamReader(_InputStream, Charset);
Reader in = new BufferedReader(isr);
int ch;
while ((ch = in.read()) > -1) {
buffer.append((char) ch);
}
response = buffer.toString();
buffer = null;
}
} catch (Exception e) {
logger.error("错误信息" + e.getMessage());
response = response + e.getMessage();
e.printStackTrace();
}
return response;
}
public static void main(String[] args) {
String url = "http://xxx?spid=3060&sppassword=hbkj3060&das=8618611178949&command=MULTI_MT_REQUEST&sm=a1beccd4b1a6a1bf20cda8b5c0bdd3c8ebcdeab3c9a3a1&dc=15";
url = "http://xxx/sdkproxy/regist.action?cdkey=2SDK-EMY-6688-AAAAA&password=123456";
// Map map = XMLConverUtil.convertToMap(doGetRequest(url));
// System.out.println(map);
}
}