您的位置:首页 > 理论基础 > 计算机网络

使用jdk自带的HttpURLConnection发送json请求

2018-03-19 11:47 471 查看
下面是发送代码的方法,目前使用的是jdk1.8测试的

/**
* 发送http POST请求
*
* @param
* @return 远程响应结果
*/
public static String sendPost(String u, String json) {
StringBuffer sbf = new StringBuffer();
try {
URL url = new URL(u);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setRequestMethod("POST");
connection.setUseCaches(false);
connection.setInstanceFollowRedirects(true);
connection.addRequestProperty("role", "Admin");
connection.addRequestProperty("Content-Type", "application/json");
connection.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
connection.connect();
DataOutputStream out = new DataOutputStream(connection.getOutputStream());
if (!"".equals(json)) {
out.writeBytes(json);
}
out.flush();
out.close();

BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String lines;
while ((lines = reader.readLine()) != null) {
lines = new String(lines.getBytes(), "utf-8");
sbf.append(lines);
}
System.out.println(sbf);
reader.close();
// 断开连接
connection.disconnect();
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generate
ae0c
d catch block
e.printStackTrace();
}
return sbf.toString();
}

public static String sendPostjason(String url, String xml) {

// File input = new File("test.xml");//如果是xml文件,可以这样写
PostMethod post = new PostMethod(url);// 请求地址

// 设置请求的内容直接从文件中读取
// post.setRequestBody( new FileInputStream(input));
// if (input.length() < Integer.MAX_VALUE)
// post.setRequestContentLength(input.length());
// else
// post.setRequestContentLength(EntityEnclosingMethod.CONTENT_LENGTH_CHUNKED);

post.setRequestBody(xml);// 这里添加xml字符串

// 指定请求内容的类型
post.setRequestHeader("Content-type", "application/json; charset=UTF-8");
post.setRequestHeader("role", "Admin");

HttpClient httpclient = new HttpClient();// 创建 HttpClient 的实例
int result;
try {
result = httpclient.executeMethod(post);
System.out.println("Response status code: " + result);// 返回200为成功
System.out.println("Response body: ");
return post.getResponseBodyAsString();// 返回的内容
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}

public static String sendGet(String u, String json) {
StringBuffer sbf = new StringBuffer();
try {
URL url = new URL(u);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.addRequestProperty("role", "Admin");
connection.addRequestProperty("Content-Type", "application/json");

BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String lines;
while ((lines = reader.readLine()) != null) {
lines = new String(lines.getBytes(), "utf-8");
sbf.append(lines);
}
System.out.println(sbf);
reader.close();
// 断开连接
connection.disconnect();
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return sbf.toString();
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: