import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import .;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
public class HttpRequestUtils {
private static Logger logger = (HttpRequestUtils.class);
private static final int connectTimeout = 1000 * 120;
private static final int socketTimeout = 1000 * 180;
public static String sendPost(String strUrl, String requestParams) {
(”sendPost strUrl:” + strUrl);
(”sendPost requestParams:” + requestParams);
URL url = null;
HttpURLConnection httpURLConnection = null;
try {
url = new URL(strUrl);
httpURLConnection = (HttpURLConnection) ();
(true);
(true);
(”POST”);
(false);
(”Accept”, “application/json”);
(”Content-Type”, “application/json”);
();
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(
(), ”UTF-8”);
(requestParams);
();
();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(
(), ”utf-8”));
StringBuffer stringBuffer = new StringBuffer();
String strLine = ”“;
while ((strLine = ()) != null) {
(strLine);
}
();
String responseParams = ();
(”sendPost responseParams:” + responseParams);
return responseParams;
} catch (IOException e) {
(”sendPost IOException:” + ());
} finally {
if (httpURLConnection != null) {
();
}
}
return null;
}
public static String doPost(String strUrl, List<BasicNameValuePair> requestParams) {
(”doPost strUrl:” + strUrl);
(”doPost requestParams:” + requestParams);
String responseParams = ”“;
StringBuffer stringBuffer = new StringBuffer();
long startTime = 0, endTime = 0;
CloseableHttpClient closeableHttpClient = ();
RequestConfig requestConfig = ()
.setConnectTimeout(connectTimeout)
.setSocketTimeout(socketTimeout)
.build();
HttpPost httpPost = new HttpPost(strUrl);
(requestConfig);
HttpEntity httpEntity;
try {
if (requestParams != null) {
httpEntity = new UrlEncodedFormEntity(requestParams, “UTF-8”);
(httpEntity);
(”doPost requestParams:” + (httpEntity));
}
startTime = ();
CloseableHttpResponse closeableHttpResponse = (httpPost);
int code = ().getStatusCode();
(”doPost 状态码:” + code);
if (code == 200 || code == 500) {
try {
httpEntity = ();
if (httpEntity != null) {
long length = ();
if (length != -1 && length < 2048) {
((httpEntity));
} else {
BufferedReader bufferedReader = new BufferedReader(
new InputStreamReader((), “UTF-8”));
String line;
while ((line = ()) != null) {
(line);
}
();
responseParams = ();
}
endTime = ();
}
} catch (Exception e) {
endTime = ();
(”doPost Exception(通讯错误):” + ());
} finally {
();
}
} else {
endTime = ();
();
(”doPost 错误请求,状态码:” + code);
}
} catch (IOException e) {
endTime = ();
(”doPost IOException:” + ());
} finally {
try {
();
} catch (IOException e) {
}
}
(”doPost 用时(毫秒):” + (endTime - startTime) / 1000000L);
(”doPost responseParams:” + responseParams);
return responseParams;
}
public static String sendGet(String strUrl, String requestParams) {
(”sendGet strUrl:” + strUrl);
(”sendGet requestParams:” + requestParams);
String responseParams = ”“;
BufferedReader bufferedReader = null;
try {
String strRequestUrl = strUrl + ”?” + requestParams;
URL url = new URL(strRequestUrl);
URLConnection urlConnection = ();
(”accept”, “*/*”);
(”connection”, “Keep-Alive”);
(”user-agent”,
”Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)”);
();
Map<String, List<String>> map = ();
bufferedReader = new BufferedReader(new InputStreamReader(
()));
String strLine;
while ((strLine = ()) != null) {
responseParams += strLine;
}
} catch (Exception e) {
(”sendGet Exception:” + ());
} finally {
try {
if (bufferedReader != null) {
();
}
} catch (Exception e2) {
();
}
}
(”sendPost responseParams:” + responseParams);
return responseParams;
}
}