/**
* 使用GET方式請求 最簡單的物流追蹤方式,若是某些物流公司的獲取追蹤信息的方式與此相同,則能夠直接調用次方法
*
* @param requestUrl
* @return 追蹤信息
*/
public static String getTrackInfo(String requestUrl) {
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpGet httpGet = new HttpGet(requestUrl);
try {
CloseableHttpResponse httpResponse = httpClient.execute(httpGet);
int statusCode = httpResponse.getStatusLine().getStatusCode();
if (statusCode != HttpStatus.SC_OK) {
System.out.println("Method is wrong " + httpResponse.getStatusLine());
return null;
}
return EntityUtils.toString(httpResponse.getEntity());
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
httpGet.releaseConnection();
httpClient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
/**
* 使用POST方式請求 最簡單的物流追蹤方式,若是某些物流公司的獲取追蹤信息的方式與此相同,則能夠直接調用次方法
*
* @param requestUrl
* @return 追蹤信息
*/
public String postTrackInfo(String requestUrl, Map<String, String> params) {
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpPost httpPost = new HttpPost(requestUrl);
try {
List<NameValuePair> nvps = new ArrayList<NameValuePair>();
if (params != null) {
for (Entry<String, String> entry : params.entrySet()) {
nvps.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
}
}
httpPost.setEntity(new UrlEncodedFormEntity(nvps, "UTF-8"));
httpPost.setHeader("Content-type", "application/x-www-form-urlencoded");
HttpResponse httpResponse = httpClient.execute(httpPost);
int statusCode = httpResponse.getStatusLine().getStatusCode();
if (statusCode != HttpStatus.SC_OK) {
System.out.println("Method is wrong " + httpResponse.getStatusLine());
return null;
}
return EntityUtils.toString(httpResponse.getEntity());
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
httpPost.releaseConnection();
httpClient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}app