zoukankan      html  css  js  c++  java
  • java-HttpURLConnection

    参考文章: https://blog.csdn.net/u014204541/article/details/79609619

    json

    url = new URL(tfsUrl);
                HttpURLConnection httpConnection = (HttpURLConnection) url.openConnection();
                httpConnection.setDoInput(true);
                httpConnection.setDoOutput(true);
                httpConnection.setUseCaches(false);
                httpConnection.setRequestProperty("Content-Type", "application/json");
                httpConnection.setRequestMethod("POST");
                httpConnection.connect();
                DataOutputStream out = new DataOutputStream(httpConnection.getOutputStream());
                if (null != fileUploadInfo) {
                    out.writeBytes(JSON.toJSONString(fileUploadInfo));
                }
                out.flush();
                out.close();
                in = httpConnection.getInputStream();
                out1 = new ByteArrayOutputStream();
                int temp = 0;
                while ((temp = in.read()) != -1) {
                        out1.write(temp);
                    }
            } catch (IOException e) {
                    e.printStackTrace();
                } finally {
                    try {
                        out1.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                String response = new String(out1.toByteArray());
                if (response == null || "".equals(response))
                    return "";
                return response;

    x-www-form-urlencoded:

    url = new URL(tfsUrl);
                HttpURLConnection httpConnection = (HttpURLConnection) url.openConnection();
                httpConnection.setDoInput(true);
                httpConnection.setDoOutput(true);
                httpConnection.setUseCaches(false);
                httpConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
                httpConnection.connect();
                DataOutputStream out = new DataOutputStream(httpConnection.getOutputStream());
                String content = "filePath=" + URLEncoder.encode(filePath, "UTF-8");
                out.writeBytes(content);
                out.flush();
                out.close();
                in = httpConnection.getInputStream();
                out1 = new ByteArrayOutputStream();
                int temp = 0;
                while ((temp = in.read()) != -1) {
                    out1.write(temp);
                }
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    out1.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            String response = new String(out1.toByteArray());
            if (response == null || "".equals(response))
                return "";
            return response;
  • 相关阅读:
    理解 RESTful:理论与最佳实践
    Shiro 性能优化:解决 Session 频繁读写问题
    单点登录的三种实现方式
    理解 Spring(二):AOP 的概念与实现原理
    理解 Spring(一):Spring 与 IoC
    MFC查内存泄漏方法
    024 --- 第28章 访问者模式
    023 --- 第27章 解释器模式
    022 --- 第26章 享元模式
    021 --- 第25章 中介者模式
  • 原文地址:https://www.cnblogs.com/DennyZhao/p/12859689.html
Copyright © 2011-2022 走看看