HTTP interface call (EBS data acquisition)

Development Background

A way to start using the same calling interface Unicom [link] interface calls , found NameValuePair way-past party accepts less than EBS parameters, so take the following way.

Interface Description

Interface DescriptionHere Insert Picture Description

Code section

Due to different business needs, request address, the parameters change, the request address in a configuration file

 /**
  *@param url:请求地址
  *@param token:密钥1
  *@param key:密钥2
  *@param data:接口所需参数
  *@param contenttype:数据格式
  * 请求EBS接口
  */
   public String requestEbs(String url, String token, String key, Object data, String contenttype) {
            String result = "";
            PrintWriter out = null;
            BufferedReader in = null;
            try {
            	//创建一个URL对象
                URL realUrl = new URL(url);
                //建立一个链接
                URLConnection conn = realUrl.openConnection();
                //设置请求密钥
                conn.addRequestProperty("token", token);
                conn.addRequestProperty("key", key)
                conn.setRequestProperty("accept", "*/*");
                conn.setRequestProperty("connection", "Keep-Alive");
                conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
                conn.setRequestProperty("Accept", "application/json");
                // 设置发送数据的格式
                conn.setRequestProperty("Content-Type", contenttype); 
                conn.setDoOutput(true);
                conn.setDoInput(true);
                out = new PrintWriter(conn.getOutputStream());
                //传入参数
                out.print(data);
                //刷新缓存区
                out.flush();
                in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
                //获取返回信息
                String line;
                while ((line = in.readLine()) != null) {
                    result += line;
                }
    
            } catch (MalformedURLException e1) {
                e1.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                out.close();
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
    
            }
            return result;
    
        }

		 private static final String EBS_PROPERTIES = "config/EBS.properties";
    	/**
         * 从配置文件读取信息
         */
        public static String getPropValue(String propKey) {
            try {
                Properties props = new Properties();
                props.load(EBSServiceImpl.class.getClassLoader().getResourceAsStream(EBS_PROPERTIES));
                return props.getProperty(propKey);
            } catch (IOException e) {
                e.printStackTrace();
            }
            return null;
        }
Published 26 original articles · won praise 6 · views 2960

Guess you like

Origin blog.csdn.net/weixin_45676630/article/details/101208022