美文网首页
代码整理——HTTP请求

代码整理——HTTP请求

作者: enjoy_CC | 来源:发表于2018-07-24 16:36 被阅读0次

使用socket套接字

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.net.Socket;

public class TestHttpClient {
    Socket socket = null;
    
    public void createSocket() {
        try {
            socket = new Socket("localhost", 8080);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    
    public void communcate() {
        // 注意这里必须制定请求方式 地址 注意空格
        StringBuffer sb = new StringBuffer("GET http://localhost:8080/xhtw/qwe.html HTTP/1.1\r\n");
        // 以下为请求头 
        sb.append("Host: localhost\r\n");
        sb.append("User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:53.0) Gecko/20100101 Firefox/53.0\r\n");
        sb.append("Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\r\n");
        sb.append("Accept-Language: zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3");
        // 注意这里不要使用压缩 否则返回乱码
        sb.append("Accept-Encoding: \r\n");
        sb.append("Connection: keep-alive\r\n");
        sb.append("Upgrade-Insecure-Requests: 1\r\n");
        // 注意这里要换行结束请求头
        sb.append("\r\n");
        System.out.println(sb.toString());
        try {
            OutputStream os = socket.getOutputStream();
            os.write(sb.toString().getBytes());
  
            InputStream is = socket.getInputStream();
            
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            byte[] bytes = new byte[1024];
            int len = -1;
            while ((len = is.read(bytes)) != -1) {
                baos.write(bytes, 0, len);
            }
            System.out.println(new String(baos.toByteArray()  ));
            socket.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    
    public static void main(String[] args) {
       TestHttpClient client = new TestHttpClient();
       client.createSocket();
       client.communcate();
        
    
    }
}

使用HttpURLConnection连接

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

public class Download {
    public static String url="http://file06.16sucai.com/2016/0927/3e3010c795e9b02fd5388d31a7c131f0.jpg";
    public static void main(String[] args) {
        
        Thread one=new Thread(()->{
            try {
                
                URL u = new URL(url);
                HttpURLConnection con=(HttpURLConnection) u.openConnection();
                con.setRequestMethod("GET");
                con.setConnectTimeout(5000);
//              con.setRequestProperty("", "");//设置
                InputStream in=con.getInputStream();
                
                ByteArrayOutputStream bout=new ByteArrayOutputStream();
                
                byte[] buffer=new byte[1024];
                int length=0;
                while((length=in.read(buffer))!=-1){
                    bout.write(buffer, 0, length);
                }
                in.close();
                
                File file=new File("E://a.jpg");
                FileOutputStream fos=new FileOutputStream(file);
                fos.write(bout.toByteArray());
                fos.flush();
                fos.close();
                
                
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            
        });

        one.start();
        
    }

}

相关文章

网友评论

      本文标题:代码整理——HTTP请求

      本文链接:https://www.haomeiwen.com/subject/ookymftx.html