欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

JAVA post和get方式请求远程HTTP接口

程序员文章站 2022-04-15 17:34:49
...

java发送http协议 一般对方都会限定post或者get 这里就是可用的方法

首先是POST方式
传入参数为json 可改为任何类型

public class HttpPost implements Runnable {

    private String url_str;

    public HttpPost () {

    }

    public HttpPost (String url, boolean isPost) {
        this.url_str = url;
    }

    @Override
    public void run() {
        JSONObject param_json = new JSONObject();

        URL url = null;
        PrintWriter send_out = null;
        BufferedReader send_in = null;
        String result = "";
        try {
            //----------------------------------------------------------//
            //http://blog.csdn.net/thl331860203/article/details/51783434//
            //JAVA POST请求远程HTTP接口                                   //
            //----------------------------------------------------------//
            url = new URL(this.url_str);
            //打开和URL之间的连接 
            URLConnection connection = url.openConnection();
            //设置通用的请求属性  
            connection.setRequestProperty("accept", "*/*");  
            connection.setRequestProperty("connection", "Keep-Alive");  
            connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); 
            connection.setRequestProperty("user-agent","Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
            end_out = new PrintWriter(connection.getOutputStream());
       //发送请求参数 
            send_out.print(param_json);
            //flush输出流的缓冲 
            send_out.flush();//定义BufferedReader输入流来读取URL的响应
            send_in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
            String line;
            while((line = send_in.readLine()) != null){
                result += line;
            }
            getResultData(result);
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void getResultData(String result) {
    }
}

接下来是Get方式

public class HttpGet implements Runnable {

    private String url_str;public HttpGet() {

    }

    public HttpGet(String url, boolean isPost) {
        this.url_str = url;
    }

    @Override
    public void run() {
        JSONObject param_json = new JSONObject();

        URL url = null;
        PrintWriter send_out = null;
        BufferedReader send_in = null;
        String result = "";
        try {
            //----------------------------------------------------------//
            //http://blog.csdn.net/thl331860203/article/details/51783434//
            //JAVA POST请求远程HTTP接口                                   //
            //----------------------------------------------------------//
            url = new URL(this.url_str);
            //打开和URL之间的连接 
            URLConnection connection = url.openConnection();
            //设置通用的请求属性  
            connection.setRequestProperty("accept", "*/*");  
            connection.setRequestProperty("connection", "Keep-Alive");  
            connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); 
            connection.setRequestProperty("user-agent","Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");//建立连接
            connection.connect();
            //定义BufferedReader输入流来读取URL的响应
            send_in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
            String line;
            while((line = send_in.readLine()) != null){
                result += line;
            }
            getResultData(result);
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void getResultData(String result) {

    }
}

关于实现

HttpGet test = new HttpGet("");
Thread thread = new Thread(test);
thread.start();

因为JAVA在某版本之后 为了防止协议没有响应 所以不允许直接使用这个方法 一定要在子线程中使用
以上除了部分代码以外都是自己的理解 如果不对 请指出 谢谢

相关标签: 安卓SDK