postman post json

POSTMAN  POST JSON

JAVA

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;

import net.sf.json.JSONObject;

public class CCB_Post_JsonParam2
{
	public static final String IP = "127.0.0.1";//
	
	public static void main(String[] args)
	{
		//
		String url = "http://" + IP + "/27dfc98a03514b0bb755b313092c57b8/tp/24/test/TC15784716205700240/1.0";
		
		JSONObject obj = new JSONObject();
		obj.put("entnm", "中国xx银行");
		obj.put("enqInf", "SSSSSS");
		obj.put("currPage", "SSSSS");
		obj.put("recInPage", "SSSSSS");
		obj.put("Ext_Acs_MgTp_Cd", "SSSSSS");
		
		//
		String result = sentPostReq(url, obj.toString());
		System.out.println(result);
	}
	
	public static String sentPostReq(String _url, String _params)
	{
		try
		{
			//
			URL url = new URL(_url);
			HttpURLConnection conn = (HttpURLConnection) url.openConnection();
			
			//
			conn.setDoOutput(true);
			conn.setDoInput(true);
			conn.setUseCaches(false);
			conn.setInstanceFollowRedirects(true);
			conn.setRequestMethod("POST"); // 设置请求方式
			conn.setRequestProperty("Accept", "*/*");
			conn.setRequestProperty("Accept-Encoding", "gzip, deflate");
			conn.setRequestProperty("Cache-Control", "no-cache");
			conn.setRequestProperty("Connection", "keep-alive");
			conn.setRequestProperty("Content-Lenth", Integer.toString(_params.length()));
			conn.setRequestProperty("Content-Type", "application/json");// 设置发送数据的格式
			conn.setRequestProperty("Host", IP);// 设置发送数据的格式
			conn.setRequestProperty("Postman-Token", "4b96-8fd6-b0c34e2eebc1");
			conn.setRequestProperty("User-Agent", "PostmanRuntime/7.15.2");
			conn.setRequestProperty("env", "test");
			conn.setRequestProperty("ak", "MFwwDow6ECAwEAAQ==");
			
			//
			conn.connect();
			
			// request
			OutputStreamWriter out = new OutputStreamWriter(conn.getOutputStream(), "UTF-8"); // utf-8编码
			out.write(_params);
			out.flush();
			out.close();
			
			// response
			BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
			String line;
			String result = "";
			while ((line = reader.readLine()) != null)
			{
				result += line;
			}
			reader.close();
			return result;
		}
		catch (IOException e)
		{
			e.printStackTrace();
		}
		
		return "-1";
	}

}

发布了380 篇原创文章 · 获赞 6 · 访问量 12万+

猜你喜欢

转载自blog.csdn.net/spencer_tseng/article/details/103992459
今日推荐