HTTPインターフェースコール(EBSデータの取得)

開発の背景

ユニコムインタフェース同じ呼び出しを使用して起動する方法[リンク]インターフェースの呼び出しは、のNameValuePair道-過去のパーティーはとても以下の方法を取る、以下EBSのパラメータよりも受け入れました。

インターフェイス説明

インターフェイス説明ここに画像を挿入説明

コードセクション

さまざまなビジネスニーズ、要求アドレス、パラメータの変更、設定ファイル内のリクエストアドレスに起因します

 /**
  *@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;
        }
公開された26元の記事 ウォンの賞賛6 ビュー2960

おすすめ

転載: blog.csdn.net/weixin_45676630/article/details/101208022