HttpsURLConnection报文头报文体使用详解

HttpsURLConnection报文头报文体使用

POST请求


    private static String TAG = "HttpConnection";
    private static String POST_URL = "https://www.baidu.com";
    private static String GET_URL = "http://www.baidu.com";
    private static String data = "{\"name\":\"Marry\",\"age\":\"17\"}";
    private static String getData = "name=Marry&age=17";

    public static void connectionPost(String appId, String timeS) {
        Log.i(TAG, "[-] connectionPost");
        try {
            URL url = new URL(POST_URL);
            HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
            //请求方法,GET,POST
            conn.setRequestMethod("POST");
            //设置从主机读取数据超时
            conn.setReadTimeout(30000);
            //设置连接主机超时
            conn.setConnectTimeout(10000);
            //读入默认为true
            conn.setDoInput(true);
            //输出 POST 设置为true,默认为false
            conn.setDoOutput(true);
            // Post 请求不能使用缓存
            conn.setUseCaches(false);
            /**setRequestProperty 为header请求头,报文头*/
            conn.setRequestProperty("Content-Type", "application/json;charset=utf-8");//Content-Type 设置为json后,报文体一定要是json字符串。默认值:application/x-www-form-urlencoded;charset=utf-8
            conn.setRequestProperty("appId ", appId);
            conn.setRequestProperty("timestamps ", timeS);
            /**
             * ContentType设置为默认值时,data要设置成key1=val1&key2=val2 如下:
             * conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");
             * data=getData;
             * */
            /**body请求体,报文体, os.write() 写入报文体内容*/
            Log.i(TAG, "请求报文体:" + data.toString().replace("\\", ""));
            OutputStream os = conn.getOutputStream();
            os.write(data.toString().replace("\\", "").getBytes("UTF-8"));
            os.flush();
            int code = conn.getResponseCode();
            if (code != 200) {
                Log.e(TAG, "[-] verfReq: verify error=网络错误");
                return;
            }
            InputStream is = conn.getInputStream();
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            byte[] buf = new byte[8192];
            int r = 0;
            while ((r = is.read(buf)) != -1) {
                baos.write(buf, 0, r);
            }
            String respStr = new String(baos.toByteArray(), "UTF-8");
            Log.d(TAG, "[+] verfReq: respStr=" + respStr);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

GET请求

public static boolean connectGET(String appId, String bizId) {
        //GET请求直接在请求地址后面拼接上请求参数
        String RESULT_URL = GET_URL + "?"+getData;
        boolean isVerifySuc = false;
        try {
            URL url = new URL(RESULT_URL);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setRequestMethod("GET");//只有报文头没有报文体
            conn.setReadTimeout(30000);
            conn.setConnectTimeout(10000);
            conn.setDoInput(true);
            conn.setDoOutput(false);
            /**header*/
            conn.setRequestProperty("Content-Type", "application/json;charset=utf-8");
            conn.setRequestProperty("appId ", appId);
            conn.setRequestProperty("bizId ", bizId);
            int code = conn.getResponseCode();
            if (code != 200) {
                Log.e(TAG, "[-] verfReq: verify error=网络错误");
                return false;
            }
            InputStream is = conn.getInputStream();
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            byte[] buf = new byte[8192];
            int r = 0;
            while ((r = is.read(buf)) != -1) {
                baos.write(buf, 0, r);
            }
            String respStr = new String(baos.toByteArray(), "UTF-8");
            Log.d(TAG, "[+] verfReq: respStr=" + respStr);
        } catch (Exception e) {
            Log.e(TAG, "[-] verfReq: verify error=", e);
        }
        return isVerifySuc;
    }

子线程中调用

必须在子线程中调用

new Thread(new Runnable() {
                    @Override
                    public void run() {

                        HttpConnection.connectGET("111","222");
                       // HttpConnection.connectionPost("111","222");
                    }
                }).start();
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值