当前位置: 技术问答>java相关
关于访问http的问题
来源: 互联网 发布时间:2015-10-10
本文导语: 如何访问http,并打印出内容,例如:java MyHttp www.sina.com.cn(MyHttp为程序)读取新浪首页的内容。 | import java.net.URL; import java.net.URLConnection; import java.io.* public class ConnectionDemo { public st...
如何访问http,并打印出内容,例如:java MyHttp www.sina.com.cn(MyHttp为程序)读取新浪首页的内容。
|
import java.net.URL;
import java.net.URLConnection;
import java.io.*
public class ConnectionDemo {
public static void main(String args[]) throws Exception {
URL url = new URL("http://www.sina.com.cn");
URLConnection conn = url.openConnection();
InputStream input = conn.getInputStream();
BufferedReader it = new BufferedReader(new InputStreamReader(input));
String str;
while((str =it.readLine())!= null){
System.out.println(str);
}
it.close();
input.close();
}
}
import java.net.URLConnection;
import java.io.*
public class ConnectionDemo {
public static void main(String args[]) throws Exception {
URL url = new URL("http://www.sina.com.cn");
URLConnection conn = url.openConnection();
InputStream input = conn.getInputStream();
BufferedReader it = new BufferedReader(new InputStreamReader(input));
String str;
while((str =it.readLine())!= null){
System.out.println(str);
}
it.close();
input.close();
}
}
|
java.net.URL l_url = new java.net.URL(/tech-qa-java/sURL/index.html);
java.net.HttpURLConnection l_connection = (java.net.HttpURLConnection) l_url.openConnection();
l_connection.connect();
l_urlStream = l_connection.getInputStream();
java.io.BufferedReader l_reader = new java.io.BufferedReader(new java.io.InputStreamReader(l_urlStream));
String sCurrentLine = "";
String sTotalString = "";
while ((sCurrentLine = l_reader.readLine()) != null)
{
sTotalString+=sCurrentLine;
}
java.net.HttpURLConnection l_connection = (java.net.HttpURLConnection) l_url.openConnection();
l_connection.connect();
l_urlStream = l_connection.getInputStream();
java.io.BufferedReader l_reader = new java.io.BufferedReader(new java.io.InputStreamReader(l_urlStream));
String sCurrentLine = "";
String sTotalString = "";
while ((sCurrentLine = l_reader.readLine()) != null)
{
sTotalString+=sCurrentLine;
}
|
sURL ="www.sina.com.cn"
sTotalString
就是所有返回内容
sTotalString
就是所有返回内容