您的位置:首页 > 理论基础 > 计算机网络

http与https请求

2015-07-23 20:42 555 查看

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;

}

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;

}

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");

}

}

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;

}



内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: