1.http请求
public static String httpGet(String path, String charsetName) throws IOException{
String rv = null;
URL url = null;
HttpURLConnection httpConnection = null;
InputStream input = null;
try {
url = new URL(path);
httpConnection = (HttpURLConnection) url.openConnection();
input = httpConnection.getInputStream();
rv = TohoursUtils.inputStream2String(input, charsetName);
} finally {
if(input != null){
input.close();
}
}
return rv;
}
String rv = null;
URL url = null;
HttpURLConnection httpConnection = null;
InputStream input = null;
try {
url = new URL(path);
httpConnection = (HttpURLConnection) url.openConnection();
input = httpConnection.getInputStream();
rv = TohoursUtils.inputStream2String(input, charsetName);
} finally {
if(input != null){
input.close();
}
}
return rv;
}
2.https请求
public static String httpsGet(String path, String charsetName) throws IOException{
String rv = null;
URL url = null;
HttpsURLConnection httpsConnection = null;
InputStream input = null;
try {
url = new URL(path);
httpsConnection = (HttpsURLConnection) url.openConnection();
input = httpsConnection.getInputStream();
rv = TohoursUtils.inputStream2String(input, charsetName);
} finally {
if(input != null){
input.close();
}
}
return rv;
}
String rv = null;
URL url = null;
HttpsURLConnection httpsConnection = null;
InputStream input = null;
try {
url = new URL(path);
httpsConnection = (HttpsURLConnection) url.openConnection();
input = httpsConnection.getInputStream();
rv = TohoursUtils.inputStream2String(input, charsetName);
} finally {
if(input != null){
input.close();
}
}
return rv;
}
3.http与https请求的调用
public static String httpGet(String path) throws IOException{
if(path.indexOf("https")>=0){
return httpsGet(path, "UTF-8");
} else {
return httpGet(path, "UTF-8");
}
}
if(path.indexOf("https")>=0){
return httpsGet(path, "UTF-8");
} else {
return httpGet(path, "UTF-8");
}
}
4.输入流转String
public static String inputStream2String(InputStream in, String charsetName) throws IOException{
StringWriter writer = new StringWriter();
IOUtils.copy(in, writer, charsetName);
String theString = writer.toString();
return theString;
}
StringWriter writer = new StringWriter();
IOUtils.copy(in, writer, charsetName);
String theString = writer.toString();
return theString;
}