java 发送 json、xml格式的 http请求,并读取响应response内容实例

java 发送 json、xml格式的 http请求,需要确定接受请求的的服务器地址(ip、端口、具体的目录)

然后设置连接属性信息

请求的数据格式(json 或 xml)

根据需要是否要向服务器发送消息体(可以只发送http头,不发具体的信息),数据格式要与设置的http头信息设置的格式一致,如果想查看发送和服务器的响应的具体信息 可以使用 http抓包工具,如 httpAnalizer等软件,查看请求数据格式是否正确。

具体代码如下(请求数据格式为json):

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;

import java.io.OutputStream;

 import java.net.HttpURLConnection;
import java.net.MalformedURLException;

 import java.net.URL;
 import org.json.JSONObject;
import org.json.JSONArray; 
 
public class AppAddTest {
 
    public static final String ADD_URL = "http://192.168.4.68:35357/v2.0/users";
 //   public static final String ADD_URL = "http://192.168.4.97:35357/v2.0/tokens";
    public static void appadd() throws IOException {
    	HttpURLConnection connection=null;
        try {
             //创建连接
             URL url = new URL(ADD_URL);
             connection = (HttpURLConnection) url.openConnection();
             

             //设置http连接属性
             
             connection.setDoOutput(true);
             connection.setDoInput(true);
             connection.setRequestMethod("POST"); // 可以根据需要 提交 GET、POST、DELETE、INPUT等http提供的功能
             connection.setUseCaches(false);
             connection.setInstanceFollowRedirects(true);
             
             //设置http头 消息
             connection.setRequestProperty("Content-Type","application/json");  //设定 请求格式 json,也可以设定xml格式的
             //connection.setRequestProperty("Content-Type", "text/xml");   //设定 请求格式 xml,
             connection.setRequestProperty("Accept","application/json");//设定响应的信息的格式为 json,也可以设定xml格式的
//             connection.setRequestProperty("X-Auth-Token","xx");  //特定http服务器需要的信息,根据服务器所需要求添加
             connection.connect();
 
             //添加 请求内容
             
            JSONObject user = new JSONObject();  
            user.put("name", "alice4");
            user.put("roles", "admin");
            user.put("enabled", "true");
            user.put("tenantId", "998abe42845e4f258da7e96b0d8335ec");
            user.put("password", "mypassword123");

            //构建嵌套的 json数据
            
            JSONObject obj = new JSONObject();
             obj.put("user",user);
             OutputStream out = connection.getOutputStream();                        
             out.write(obj.toString().getBytes());
             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) throws IOException {
         appadd();
     }
 
}


 

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值