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;
  • 相关阅读:
    [LCA] 最近公共祖先
    [DP] D. Beautiful Array
    [模板] [拓扑序列]
    [模板] 区间筛素数
    [DP] 简单的烦恼
    [贪心] 二元组最小值最大
    [模板] 树状数组及其应用
    [Trie] 最大异或对
    [模板][二分]倍增及其应用
    ios学习记录 day31 UI 9 多视图切换 导航控制器
  • 原文地址:https://www.cnblogs.com/DennyZhao/p/12859689.html
Copyright © 2011-2022 走看看