Java 模拟 GET 与 POST 请求

模拟 get 请求

package com.woniuxy.httpdemo;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

public class SendGetDemo {
    
    public static void main(String[] args) {
        // 准备URL
        String urlS = "http://192.168.11.52/Agileone_1.2/index.php";
        
        HttpURLConnection con = null;
        InputStream inputStream = null;
        
        // 
        String key = "Cookie";
        String value = "username=admin; password=admin; PHPSESSID=14441ab0acae1ed0cef2f332ba4b0e85";
        
        try {
            URL url = new URL(urlS);
            // 获取 HTTP 连接对象
            con = (HttpURLConnection) url.openConnection();
            // 设置请求头信息
            con.setRequestProperty(key, value);
            // 建立连接
            con.connect();
            
            // 接收服务器相应内容
            inputStream = con.getInputStream();
            // 长度
            int len = -1;
            // 容器
            byte[] response = new byte[1024];
            
            while ((len = inputStream.read(response)) != -1) {
                System.out.println(new String(response, 0, len, "UTF-8"));
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                inputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            con.disconnect();;
            
        }
    }
}

模拟 post 请求

package com.woxiuxy.httpdemo;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

public class POSTDemo {
    public static void main(String[] args) {
        // URL
        String URLStr = "http://192.168.10.136/Agileone_1.2/index.php/common/login";
        OutputStream os = null;
        InputStream is = null;
        HttpURLConnection con = null;
        // 封装URL对象
        try {
            URL url = new URL(URLStr);
            // 获取HTTP连接
            con = (HttpURLConnection) url.openConnection();
            // 设置请求参数
            // 设置请求方式
            con.setRequestMethod("POST");
            // 设置Cookie
            con.setRequestProperty("Cookie", "PHPSESSID=c05026c0c5131dac7d880ae2a811f339");
            // 开启OutPut流
            con.setDoOutput(true);
            // 获取输出流
            os = con.getOutputStream();
            // 建立连接
            con.connect();
            // 定义请求正文
            String requestContent = "username=admin&password=admin&savelogin=true";
            // 发送数据
            os.write(requestContent.getBytes());
            
            
            // 获取服务器响应
            // 获取输入流
            is = con.getInputStream();
            // 读取响应数据
            byte[] content = new byte[1024];
            int len = -1;
            while ((len = is.read(content)) != -1) {
                System.out.print(new String(content, 0, len, "UTF-8"));
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (is != null) {
                    is.close();
                }
                if (os != null) {
                    is.close();
                }
                if (con != null) {
                    con.disconnect();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}


最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容