java拼接请求

来源:互联网 发布:ios开发用什么数据库 编辑:程序博客网 时间:2024/05/20 15:11
private JSONObject sendHttpsRequest(String pUrl,JSONObject postData)throws NoSuchAlgorithmException, KeyManagementException,IOException, JSONException {SSLContext sc = SSLContext.getInstance("SSL");sc.init(null, new TrustManager[] { new TrustAnyTrustManager() },new java.security.SecureRandom());StringBuffer mySign = new StringBuffer("");YoutuSign.appSign(APP_ID, SECRET_ID, SECRET_KEY,System.currentTimeMillis() / 1000 + EXPIRED_SECONDS,"", mySign);System.setProperty("sun.net.client.defaultConnectTimeout", "30000");System.setProperty("sun.net.client.defaultReadTimeout", "30000");//需要请求的地址URL url = new URL(pUrl);HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();connection.setSSLSocketFactory(sc.getSocketFactory());connection.setHostnameVerifier(new TrustAnyHostnameVerifier());     //请求方式采用post请求connection.setRequestMethod("POST");connection.setRequestProperty("accept", "*/*");connection.setRequestProperty("user-agent", "youtu-java-sdk");connection.setRequestProperty("Authorization", mySign.toString());connection.setDoOutput(true);connection.setDoInput(true);connection.setUseCaches(false);connection.setInstanceFollowRedirects(true);connection.setRequestProperty("Content-Type", "text/json");connection.connect();    // POST请求DataOutputStream out = new DataOutputStream(connection.getOutputStream());postData.put("app_id", APP_ID);out.write(postData.toString().getBytes("utf-8"));// 刷新、关闭out.flush();out.close();// 读取响应BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));String lines;StringBuffer resposeBuffer = new StringBuffer("");while ((lines = reader.readLine()) != null) {lines = new String(lines.getBytes(), "utf-8");resposeBuffer.append(lines);}     // System.out.println(resposeBuffer+"\n");reader.close();     // 断开连接connection.disconnect();JSONObject respose = JSONObject.fromObject(resposeBuffer.toString());return respose;}

原创粉丝点击