Jave 模拟 http 请求

来源:互联网 发布:网络摄像机远程重启 编辑:程序博客网 时间:2024/05/21 08:57

在编写一个爬虫,当然必须要模拟http请求了!

于是又了下面的代码:

import java.io.BufferedReader;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.io.OutputStreamWriter;import java.io.*;import java.net.URL;import java.net.URLConnection;public class httpTest {public static void testPost() throws IOException {           URL url = new URL("https://foursquare.com/v/universal-studios-singapore/4b1ee9ebf964a5207e2124e3");           //set the url         URLConnection connection = url.openConnection();                   connection.setDoOutput(true);                     RandomAccessFile ra = new RandomAccessFile("E:\\html.html", "rw");        //set the out filename and path        String sCurrentLine;           String sTotalString;           sCurrentLine = "";           sTotalString = "";           InputStream l_urlStream;           l_urlStream = connection.getInputStream();                      BufferedReader l_reader = new BufferedReader(new InputStreamReader(                   l_urlStream));           while ((sCurrentLine = l_reader.readLine()) != null) {               sTotalString += sCurrentLine + "\r\n";           //sTotalString is the out put stream.        }                   System.out.println(sTotalString);         ra.writeBytes(sTotalString);        ra.close();    }         public static void main(String[] args) throws IOException {           testPost();       }   }

最终的接受到的html文件在E盘的html.html文件里面。

大家有没有什么好的关于java网络编程的书

比较系统的那种。谢谢!


原创粉丝点击