http编程(一)使用javaAPI实现

Java Http编程中常见的实现方式是使用Java 提供的API,另外就是使用Apache提供的 API

1、通过Java提供的API实现Http编程
  类:URL:类 URL 代表一个统一资源定位符,它是指向互联网“资源”的指针。
      HttpURLConnection:支持 HTTP 特定功能的 URLConnection
      URLConnection 抽象类是所有类的超类,它代表应用程序和 URL 之间的通信链接。此类的实例可用于读取和写入此 URL 引用的资源

  1.1、下载数据(以下载一直图片为例)
 import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

public class DownloadImage {
    
    public static void main(String[] args) throws IOException {
        //资源的URL:就是一个资源的连接,URL中的参数淡然也可以是网上的一些图片或者其他资源的连接了
        //例如把http://localhost:8080/Day_0818/aa.jpg换为http://home.cnblogs.com/images/logo_home.gif下载博客园的logo,当然存储 到时候要改后缀了
        URL url = new URL("http://localhost:8080/Day_0818/aa.jpg");
        //通过url获取一个封装了http协议的URL连接对象:HttpURLConnection
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        //设置连接的请求方式,因为是获取数据,所以请求方式为GET:必须大写
        connection.setRequestMethod("GET");
        //设置是否能够获取连接的输入流,默认就是true,也可以不写这条语句
        connection.setDoInput(true);
        //有了连接,就要打开连接
        connection.connect();
        //获取响应码
        int code = connection.getResponseCode();
        //响应码是200则表示连接成功响应
        if(200 == code){
            //获取连接 的输入流
            InputStream is = connection.getInputStream();
            //文件输出流对象,(创建存放资源的文件)
            FileOutputStream fos = new FileOutputStream("e:\aa.jpg");
            //字节数组,我理解为输入流和输出流的一个中介,输入流把数据放到数组里让输出流读取
            byte[] b = new byte[1024];
            int length = -1;
            while((length = is.read(b)) != -1){
                fos.write(b, 0, length);
                fos.flush();
            }
            //关闭流
            fos.close();
        }
    }

}
----------------------------------------------------------------------------------------
//post方式来模拟登录。
/*
需要创建LoginServlet类接收数据
*/
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;

//http://localhost:8080/MyServer/loginServlet?username=admin&userpwd=111
public class URLDemo2 {
    public static void main(String[] args) throws Exception {
        String path = "http://localhost:8080/MyServer/loginServlet";
        URL url = new URL(path);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setRequestMethod("POST");
        connection.setConnectTimeout(30000);
        connection.setDoInput(true);
        connection.setDoOutput(true);
        //username=admin&userpwd=111
        /*
         * 将用户名和密码改成用户输入的数据。
         */
        OutputStream os = connection.getOutputStream();
        os.write("username=admin&userpwd=111".getBytes());
        connection.connect();
        int code = connection.getResponseCode();
        if(code==200){
            InputStream is = connection.getInputStream();
            byte[] b = new byte[1024];
            int length = is.read(b);
            System.out.println(new String(b,0,length));
            is.close();
        }
        
    }

}




  
原文地址:https://www.cnblogs.com/xinge1993/p/4769688.html