HttpsURLConnection报文头报文体使用详解

时间: 2023-09-30 admin IT培训

HttpsURLConnection报文头报文体使用详解

HttpsURLConnection报文头报文体使用详解

HttpsURLConnection报文头报文体使用

  • POST请求
  • GET请求
  • 子线程中调用
    • 必须在子线程中调用

POST请求

private static String TAG = "HttpConnection";private static String POST_URL = "";private static String GET_URL = "";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,POSTconn.setRequestMethod("POST");//设置从主机读取数据超时conn.setReadTimeout(30000);//设置连接主机超时conn.setConnectTimeout(10000);//读入默认为trueconn.setDoInput(true);//输出 POST 设置为true,默认为falseconn.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-8conn.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() {@Overridepublic void run() {HttpConnection.connectGET("111","222");// HttpConnection.connectionPost("111","222");}}).start();