Thread visitbaiduthread = new Thread (new visitwebrunnable ());
Visitbaiduthread.start ();
try {
Visitbaiduthread.join ();
System.out.println ("xiancheng======");
} catch (Interruptedexception e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
----------------------
Class Visitwebrunnable implements runnable{
@Override
public void Run () {
TODO auto-generated Method Stub
String data = Geturlresponse ("http://www.bjtime.cn");
String resultstr = data;
System.out.println ("ddddd----" +resultstr);
}
private string Geturlresponse (String urlstring) {
HttpURLConnection conn = null; Connection object
InputStream is = null;
Resultdata = "";
try {
URL url = new URL (urlstring); URL Object
conn = (httpurlconnection) url.openconnection (); Open a link using a URL
Conn.setdoinput (TRUE); Allow input stream, that is, allow download
Conn.setdooutput (TRUE); Allow output stream, i.e. allow upload
Conn.setusecaches (FALSE); Do not use buffering
Conn.setrequestmethod ("GET"); Using GET Requests
is = Conn.getinputstream (); Gets the input stream, at which point the link is actually established
InputStreamReader ISR = new InputStreamReader (IS);
BufferedReader bufferreader = new BufferedReader (ISR);
String inputline = "";
while ((Inputline = Bufferreader.readline ()) = null) {
Resultdata + = Inputline + "\ n";
}
} catch (Malformedurlexception e) {
TODO auto-generated Catch block
E.printstacktrace ();
}catch (IOException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}finally{
if (is = null) {
try {
Is.close ();
} catch (IOException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
}
IF (conn! = null) {
Conn.disconnect ();
}
}
return resultdata;
}