首页 > 编程语言 > 详细

java-HttpURLConnection

时间:2020-05-09 20:55:56      阅读:56      评论:0      收藏:0      [点我收藏+]

参考文章: 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;

 

java-HttpURLConnection

原文:https://www.cnblogs.com/DennyZhao/p/12859689.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!