-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathUrlTest.java
More file actions
71 lines (58 loc) · 2.31 KB
/
UrlTest.java
File metadata and controls
71 lines (58 loc) · 2.31 KB
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
59
60
61
62
63
64
65
66
67
68
69
70
71
package com.wiceflow.url;
import com.alibaba.fastjson.JSON;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
/**
* @author BF
* @date 2018/8/19
* 测试外部API调用
*/
public class UrlTest {
public static void main(String args[]) throws IOException {
String urlPath = "http://a1.easemob.com/1161180522177157/emergencysecurity/token";
Param param = new Param();
param.setGrant_type("client_credentials");
param.setClient_id("YXA6QeOkkGOtEeiS5c3cG5vPcQ");
param.setClient_secret("YXA653W-s3uWHxv7E9Q0fRBzVrDGWz4");
String paramString = JSON.toJSONString(param);
//建立连接
URL url=new URL(urlPath);
HttpURLConnection httpConn=(HttpURLConnection)url.openConnection();
//设置参数 //需要输出
httpConn.setDoOutput(true);
//需要输入
httpConn.setDoInput(true);
// 不允许缓存
httpConn.setUseCaches(false);
// 设置POST方式连接 默认为false
httpConn.setRequestMethod("POST");
// 设置请求属性
httpConn.setRequestProperty("Content-Type", "application/json");
// 维持长连接
httpConn.setRequestProperty("Connection", "Keep-Alive");
httpConn.setRequestProperty("Charset", "UTF-8");
//连接,也可以不用明文connect,使用下面的httpConn.getOutputStream()会自动connect
httpConn.connect();
//建立输入流,向指向的URL传入参数
DataOutputStream dos = new DataOutputStream(httpConn.getOutputStream());
dos.writeBytes(paramString);
dos.flush();
dos.close();
//获得响应状态
int resultCode=httpConn.getResponseCode();
if(HttpURLConnection.HTTP_OK==resultCode){
StringBuilder sb=new StringBuilder();
String readLine =new String();
BufferedReader responseReader=new BufferedReader(new InputStreamReader(httpConn.getInputStream(),"UTF-8"));
while((readLine=responseReader.readLine())!=null){
sb.append(readLine).append("\n");
}
responseReader.close();
System.out.println(sb.toString());
}
}
}