https请求

把参数装到map里面,然后传入https请求。
此方法支持get和post请求。

Map<String ,String> map = new Hashmap<String,String>();
map.put("appKey",appKey);
map.put("randomCode",randomCode);
    /**
    *https上传合并get个post版本
    *1.url上传路径
    *2.仅限POST》 param 请求参数,请求参数应该是name1=value1&name2=value2的形式。
    *3.type  上传类型  必须大写  GET / POST
    *4.appKey》》需要配置
    *5.randomCode》》10位的随机值,大小写字母和数字组成    例如:2DP3Ud2xzq
    *6.encryptedValue 》》MD5(appSecret+randomCode)
    *7.accessToken>>token
    */

    public static JSONObject httpsGetAndPost(String url,String param,String type, String appKey ,String randomCode,String encryptedValue,String accessToken)
        {
            PrintWriter out = null;
            BufferedReader in = null;
            JSONObject jsonObject = null;
            StringBuffer buffer = new StringBuffer();
            HttpsURLConnection conn = null;
            try
            {
                URL realUrl = new URL(url);
                log.out("-----------httpsGetAndPost==>url="+url+"-------------");
                // 打开和URL之间的连接
                conn = (HttpsURLConnection)realUrl.openConnection();
                conn.setSSLSocketFactory(new TLSSocketConnectionFactory());
                log.out("-----------httpsGetAndPost==1-------------");
                //HttpURLConnection conn = (HttpURLConnection) realUrl.openConnection();
                //conn.setHostnameVerifier(new CustomizedHostnameVerifier());
                    log.out("-----------httpsGetAndPost==2-------------");
                // 设置通用的请求属性
                //conn.setRequestProperty("accept", "*/*");
                log.out("-----------httpsGetAndPost==3-------------");
                //conn.setRequestProperty("Connection", "Keep-Alive");
                log.out("-----------httpsGetAndPost==4-------------");
                conn.setRequestProperty("Content-Type", "Application/json");
                if(appKey != null){
                    conn.setRequestProperty("appKey", appKey);
                }
                if(randomCode !=null){
                    conn.setRequestProperty("randomCode", randomCode);
                }
                if(encryptedValue !=null){
                    conn.setRequestProperty("encryptedValue", encryptedValue);
                }
                conn.setConnectTimeout(300000);
                conn.setReadTimeout(300000);
                log.out("-----------httpRequest1==5-------------");
                if(accessToken != null) {
                    log.out("-----------httpRequest1==6-------------");
                    conn.setRequestProperty("Authorization", "Bearer "+accessToken);
                }
                conn.setDoOutput(true);
                conn.setDoInput(true);
                // 发送POST请求必须设置如下两行
                log.out("-----------httpRequest1==7-------------");
                conn.setRequestMethod(type);
                if(type.equals("GET")){
                    conn.connect();
                }
                if(type.equals("POST")){
                    log.out("header==>" + conn.getRequestProperties());
                    log.out("Authorization==>" + conn.getRequestProperty("Authorization"));
                    log.out("POST param===>" + param);
                    // 获取URLConnection对象对应的输出流
                    log.out("-----------httpRequest1==8-------------");
                    out = new PrintWriter(conn.getOutputStream());
                    // 发送请求参数
                    log.out("-----------httpRequest1==9-------------");
                    out.print(param);
                    // flush输出流的缓冲
                    out.flush();
                }
                log.out("-----------httpRequest1==10-------------");
                int code = conn.getResponseCode();
                System.out.println(code);
                InputStream inputStream = null;
                if(code == 200) {
                    inputStream = conn.getInputStream();
                } else {
                    inputStream = conn.getErrorStream();
                }  
                // 定义BufferedReader输入流来读取URL的响应
                in = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));
                log.out("-----------httpRequest1==11-------------");
                String line;
                while ((line = in.readLine()) != null)
                {
                    log.out("-----------httpRequest1==12-------------");
                    buffer.append(line);
                }
                log.out("-----------httpRequest1==13-------------");
                jsonObject = new JSONObject().fromObject(buffer.toString());
                log.debug("-----getHeaderFields:" + conn.getHeaderFields().toString());
                log.debug("-----将返回的jsonObject:" + jsonObject.toString());
            }
            catch (Exception e)
            {
                log.error("发送POST请求出现异常!", e);
            }
            // 使用finally块来关闭输出流、输入流
            finally
            {
                try
                {
                    if (out != null)
                    {
                        out.close();
                    }
                    if (in != null)
                    {
                        in.close();
                    }
                    if(conn != null) {
                        conn.disconnect();
                    }
                }
                catch (IOException ex)
                {
                    log.error("post流关闭异常", ex);
                }
            }
            return jsonObject;
        }

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值