62,628
社区成员
发帖
与我相关
我的任务
分享
try {
URL u=new URL("http://tech.qq.com/web/it/telerss.xml");
String host=u.getHost();
String path=u.getPath();
String param=u.getQuery();
try {
Socket s=new Socket(host,80);
s.setKeepAlive(true);//长连接
OutputStream os=s.getOutputStream();
PrintWriter osOut=new PrintWriter(os,true);
//InputStream osIn=s.getInputStream();
BufferedReader osIn=new BufferedReader(
new InputStreamReader(s.getInputStream(),Charset.forName("GB2312")));
osOut.println("GET "+path+"?"+param+" HTTP/1.1");
osOut.println("Host: "+host+"");
osOut.println("User-Agent: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.76 Safari/537.36");
osOut.println("Accept-Charset: iso-8859-1, unicode-1-1;q=0.8");
osOut.println("Accept-Language: en-us,en;q=0.8");
osOut.println("Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5");
osOut.println("Accept-Encoding: gzip,deflate");
osOut.println("Connection: Close");
osOut.println();
//get response
StringBuilder sb=new StringBuilder();
int c;
while((c=osIn.read())!=-1){
sb.append((char)c);
}
s.close();
System.out.println(sb.toString());
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}