包括使用HttpURLConnection执行get/post请求

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package com.cn.testproject;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
public class HttpConnectionUrlDemo {
    public static void main(String[] args) throws Exception {
        //get();
        post();

    }

 

    public static void get() throws Exception {
        String path = "http://www.baidu.com";
        URL url = new URL(path);
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setConnectTimeout(5 * 1000);
        conn.setRequestMethod("GET");
        InputStream inStream = conn.getInputStream();
        byte[] data = toByteArray(inStream);
        String result = new String(data, "UTF-8");
        System.out.println(result);

    }

 

    public static void post() throws Exception {
        String encoding = "UTF-8";
        //post的form参数(json兼职对)
        String params = "[{\"addTime\":\"2011-09-19 14:23:02\"[],\"iccid\":\"1111\",\"id\":0,\"imei\":\"2222\",\"imsi\":\"3333\",\"phoneType\":\"4444\",\"remark\":\"aaaa\",\"tel\":\"5555\"}]";
        String path = "http://www.baidu.com";
        byte[] data = params.getBytes(encoding);
        URL url = new URL(path);
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setRequestMethod("POST");
        conn.setDoOutput(true);
     
        conn.setRequestProperty("Content-Type", "application/x-javascript; charset=" + encoding);
        conn.setRequestProperty("Content-Length", String.valueOf(data.length));
        conn.setConnectTimeout(5 * 1000);
        OutputStream outStream = conn.getOutputStream();
        outStream.write(data);
        outStream.flush();
        outStream.close();
        System.out.println(conn.getResponseCode()); // 响应代码 200表示成功
        if (conn.getResponseCode() == 200) {
            InputStream inStream = conn.getInputStream();
            String result = new String(toByteArray(inStream), "UTF-8");
            System.out.println(result); // 响应代码 200表示成功
        }
    }
    private static byte[] toByteArray(InputStream input) throws IOException {
        ByteArrayOutputStream output = new ByteArrayOutputStream();
        byte[] buffer = new byte[4096];
        int n = 0;
        while (-1 != (n = input.read(buffer))) {
            output.write(buffer, 0, n);
        }
        return output.toByteArray();
    }
}

Java_HttpURLConnection使用的更多相关文章

随机推荐

  1. uGUI练习(八) InputField

    InputField 文本输入组件,本文练习InputField的属性及事件 一.属性 1.Character Limit 限制字符长度(0表示不限制),比如:设置只能输入3个字符(中文,英文,数字, ...

  2. 通过输入卡号前10位数字判断是哪个银行的卡和类型(储蓄卡or信用卡)

    19位银行卡(包括储蓄卡和信用卡)可以通过前10位数字判断是哪个银行的卡和类型(储蓄卡or信用卡) 16位银行卡(包括储蓄卡和信用卡)可以通过前10位数字判断是哪个银行的卡和类型(储蓄卡or信用卡) ...

  3. C# Thread.Join()用法的理解 转

    指在一线程里面调用另一线程join方法时,表示将本线程阻塞直至另一线程终止时再执行 比如 1using System; 2 3namespace TestThreadJoin 4{ 5 class P ...

  4. C# is as

    if(obj is ClassA) //遍历类层次,看OBJ是不是ClassA类型{    ClassA a=(ClassA) obj; //遍历类层次,看obj能否转换为ClassA,不成功则抛出异 ...

  5. bzoj roll题器(Py大法好)

    此roll题器能实现非权限号跳过权限题及所有号跳过已切题 点击下载rar文件(py2) 点击下载rar文件(py3)

  6. Difference Between TIF and TIFF

    TIF vs TIFF Many people are confused with similar file extensions that only differ by a single lette ...

  7. nginx缓存模块配置总结proxy_cache(未完)

    简介:此缓存设置用到了第三方模块purge,使用的时候就在源链接和访问的具体内容之间加入关键字"/purge/"即可. 如:访问http://192.168.0.1/a.png 会 ...

  8. [iOS翻译]《iOS7 by Tutorials》系列:在Xcode 5里使用单元测试(上)

    简介: 单元测试是软件开发的一个重要方面.毕竟,单元测试可以帮你找到bug和崩溃原因,而程序崩溃是Apple在审查时拒绝app上架的首要原因. 单元测试不是万能的,但Apple把它作为开发工具包的一部 ...

  9. react-native 的微信SDK辅助包,支持微信登录、微信分享、微信支付

    微信SDK集成示例,现已完成微信授权登录,之后将陆续包装分享等其他功能. ReactNative高级交流群 127482131 或访问  http://blog.1ygowu.com ReactNat ...

  10. Opencv step by step - 图像融合

    两个图像的融合就是像素的融合了,其实手动操作即可,用函数操作更方便了. 下面代码的作用是融合阿狸和doctor,很和谐有木有! #include <cv.h> #include <h ...