import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
/**
* java访问url
*
*/
public class JavaAccessUrl {
public static String getUrlContent(String str){
BufferedReader read = null;
InputStream input = null;
StringBuffer sb = new StringBuffer();
String line = "";
try {
URL url = new URL(str);
HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();
httpConn.setConnectTimeout(5000);//设置连接服务器的超时时间
httpConn.setReadTimeout(3000);//设置从服务器读取数据的超时时间
httpConn.connect();
input = httpConn.getInputStream();
read = new BufferedReader(new InputStreamReader(input, "utf-8"));
while ((line = read.readLine()) != null) {
sb.append(line);
}
} catch (Exception e) {
e.printStackTrace();
}
String result = sb.toString();
return result;
}
}
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
/**
* java访问url
*
*/
public class JavaAccessUrl {
public static String getUrlContent(String str){
BufferedReader read = null;
InputStream input = null;
StringBuffer sb = new StringBuffer();
String line = "";
try {
URL url = new URL(str);
HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();
httpConn.setConnectTimeout(5000);//设置连接服务器的超时时间
httpConn.setReadTimeout(3000);//设置从服务器读取数据的超时时间
httpConn.connect();
input = httpConn.getInputStream();
read = new BufferedReader(new InputStreamReader(input, "utf-8"));
while ((line = read.readLine()) != null) {
sb.append(line);
}
} catch (Exception e) {
e.printStackTrace();
}
String result = sb.toString();
return result;
}
}