后端发送URL请求

1、发送请求

public InputStream sendUrl(String url)throws Exception{

         HttpURLConnection conn = (HttpURLConnection) new URL(url)
                    .openConnection();
         //可以设置很多属性
         conn.setRequestMethod("POST");
         conn.setReadTimeout(300);
         conn.setConnectTimeout(1000);
         if (conn.getResponseCode() == 200) {
             InputStream inputStream = conn.getInputStream();
             return inputStream;
         }   
         return null;
}

2、接收请求

 InputStream stream = sendUrl("http://www.baidu.com");
        byte [] read=new byte[50];
        int read1=0;
        while (read1!=-1){
            read1= stream.read(read);
            for (int i=0;i

 

你可能感兴趣的:(java编程)