Java_HttpURLConnection使用
包括使用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 { 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\"}]"; 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使用的更多相关文章
随机推荐
- 循环队列+堆优化dijkstra最短路 BZOJ 4152: [AMPPZ2014]The Captain
循环队列基础知识 1.循环队列需要几个参数来确定 循环队列需要2个参数,front和rear 2.循环队列各个参数的含义 (1)队列初始化时,front和rear值都为零: (2)当队列不为空时,fr ...
- codeforces 577B B. Modulo Sum(水题)
题目链接: B. Modulo Sum time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- js立即执行函数: (function ( ){...})( ) 与 (function ( ){...}( )) 有区别?
没有区别. 你需要明白 IIFE 的原理,我简单说一下: function foo() {...} // 这是定义,Declaration:定义只是让解释器知道其存在,但是不会运行. foo(); / ...
- python中*args和**args的不同
上一段代码,大家感受一下 def test_param(*args): print(args) def test_param2(**args): print(args) test_param('tes ...
- HDU 4998 Rotate --几何
题意:给n个点(x,y,p),从1~n,一次每次所有点绕着第 i 个点(原来的)逆时针转pi个弧度,问最后所有点的位置相当于绕哪个点旋转多少弧度,求出那点X和弧度P 解法:直接模拟旋转,每次计算新的坐 ...
- C#往线程里传递参数
Thread (ParameterizedThreadStart) 初始化 Thread 类的新实例,指定允许对象在线程启动时传递给线程的委托. Thread (ThreadStart) 初始化 Th ...
- ArcGIS Engine 中 线加箭头
; ICartographicLineSymbol pCartoLineSymbol = ); IArrowMarkerSymbol p ...
- addEvent和removeEvent优化写法
;(function(){ /** * 初始化分支是一种优化模式,当知道某个条件在整个生命周期内都不会发生变化时,仅对该条件测试一次. */ // 一般写法 var util = { addEvent ...
- 项目管理和缺陷跟踪工具Redmine
官网: http://www.redmine.org/ http://demo.redmine.org/ 下载: http://www.redmine.org/projects/redmine/wik ...
- Android 判断现在系统存储器是“手机存储”还是“SD存储”
import android.os.storage.StorageManager; String fileDir = null; StorageManager storageMa ...