package com.xin.web.action;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import net.sf.json.JSONObject;
public class ImportData {
public static final String BASE_URL = "https://iot.wuliancloud.com/sso/";
public static final String GET_TOKEN_URL = BASE_URL + "partner/tokens";
public static void appadd() {
try {
// 创建连接
URL url = new URL(GET_TOKEN_URL);
HttpURLConnection connection = (HttpURLConnection) url
.openConnection();
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setRequestMethod("POST");
connection.setUseCaches(false);
connection.setInstanceFollowRedirects(true);
// connection.setRequestProperty("Content-Type",
// "application/x-www-form-urlencoded");
connection.setRequestProperty("Content-Type", "application/json");
connection.connect();
// POST请求
DataOutputStream out = new DataOutputStream(
connection.getOutputStream());
JSONObject obj = new JSONObject();
obj.element("partnerId", "*************");
obj.element("partnerKey", "****************");
out.writeBytes(obj.toString());
out.flush();
out.close();
// 读取响应
BufferedReader reader = new BufferedReader(new InputStreamReader(
connection.getInputStream()));
String lines;
StringBuffer sb = new StringBuffer("");
while ((lines = reader.readLine()) != null) {
lines = new String(lines.getBytes(), "utf-8");
sb.append(lines);
}
System.out.println(sb);
reader.close();
// 断开连接
connection.disconnect();
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args) {
appadd();
}
}
Java Json Post 请求
最新推荐文章于 2024-02-27 14:01:03 发布