2024年3月21日发(作者:)

java代码发送JSON格式的httpPOST请求

package ;

import edReader;

import tputStream;

import ption;

import treamReader;

import ortedEncodingException;

import LConnection;

import medURLException;

import ;

import ject;

public class AppAddTest {

public static final String ADD_URL = "";

public static void appadd() {

try {

//创建连接

URL url = new URL(ADD_URL);

HttpURLConnection connection = (HttpURLConnection) url

.openConnection();

utput(true);

nput(true);

uestMethod("POST");

Caches(false);

tanceFollowRedirects(true);

uestProperty("Content-Type",

"application/x-www-form-urlencoded");

t();

//POST请求

DataOutputStream out = new DataOutputStream(

putStream());

JSONObject obj = new JSONObject();

t("app_name", "asdf");

t("app_ip", "10.21.243.234");

t("app_port", 8080);

t("app_type", "001");

t("app_area", "asd");

ytes(ng());

();

();

//读取响应

BufferedReader reader = new BufferedReader(new InputStreamReader(

utStream()));

String lines;

StringBuffer sb = new StringBuffer("");

while ((lines = ne()) != null) {

lines = new String(es(), "utf-8");

(lines);

}

n(sb);

();

// 断开连接

nect();

} catch (MalformedURLException e) {

// TODO Auto-generated catch block

tackTrace();

} catch (UnsupportedEncodingException e) {

// TODO Auto-generated catch block

tackTrace();

} catch (IOException e) {

// TODO Auto-generated catch block

tackTrace();

}

}

public static void main(String[] args) {

appadd();

}

}