【问题标题】:How to POST xml data while calling API through JAVA?通过 JAVA 调用 API 时如何发布 xml 数据?
【发布时间】:2017-07-10 09:44:08
【问题描述】:

我正在创建一个应用程序,我想将 xml 数据直接(没有键值对)发布到 API。 API 需要在代码中完成的认证认证。现在我想将数据发布到相同的 URL。

这是我想做的:

我当前的代码是:

@Override
    public String demoAPI(String xmlData) {
        StringBuilder sb = new StringBuilder();
        String output="";
    try {
        KeyStore clientStore = KeyStore.getInstance("PKCS12");
        clientStore.load(new FileInputStream(new File("path-to-pfx-file")),
                "password".toCharArray());
        KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
        kmf.init(clientStore, "password".toCharArray());
        KeyManager[] kms = kmf.getKeyManagers();
        KeyStore trustStore = KeyStore.getInstance("JKS");
        trustStore.load(new FileInputStream("path-to-jks-file"), "password".toCharArray());
        TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
        tmf.init(trustStore);
        TrustManager[] tms = tmf.getTrustManagers();
        final SSLContext sslContext = SSLContext.getInstance("TLS");
        sslContext.init(kms, tms, new SecureRandom());
        SSLContext.setDefault(sslContext);
        HostnameVerifier hostnameVerifier = NoopHostnameVerifier.INSTANCE;
        HttpsURLConnection.setDefaultSSLSocketFactory(sslContext.getSocketFactory());
        HttpsURLConnection.setDefaultHostnameVerifier(hostnameVerifier);
        URL url = new URL("URL");
        HttpsURLConnection con = (HttpsURLConnection) url.openConnection();
        con.setRequestMethod("GET");
        con.setConnectTimeout(100000);
        con.setSSLSocketFactory(sslContext.getSocketFactory());
        con.connect();
        BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream()));
        String line;
        while ((line = br.readLine()) != null) {
            sb.append(line + "\n");
        }
        br.close();
        System.out.println(sb.toString());
        output = sb.toString();
    } catch (Exception e) {
        e.printStackTrace();
        output = e.getMessage();
    }
    return output;
}

然而,对于相同的 url,我想从方法中发布 xmlData。 我怎样才能做到这一点 ?之后我应该在代码中更改什么 con.setRequestMethod("POST"); ?

【问题讨论】:

  • 你尝试的时候发生了什么?
  • 我在问如何在 con.setRequestMethod("POST"); 之后发布数据假设,我已将我的方法编辑为: public String demoAPI(String xmlData) 。现在,如何将这个 xmlData 发布到这个 URL?

标签: java xml post certificate logic


【解决方案1】:

你有一个 url,你有一些 XML 可以通过 HTTP 发布这个 url。而您的 post 请求是一个 SOAP 请求,您可以在此请求的正文中发送此 soap 消息。因此,与许多开发人员所做的没有什么特别或不同的。使用java.netApache 或其他方式通过http 发布您的soap 数据。以下是执行此操作的代码示例:

HttpsURLConnection con = null;
try{
    URL url = new URL("URL");
    con = (HttpsURLConnection) url.openConnection();
    con.setRequestMethod("POST");
    con.setRequestProperty("content-type", "application/x-www-form-urlencoded");
    con.setRequestProperty("Content-Language", "application/soap+xml; charset=utf-8"); // request properties, set your needs
    con.setDoOutput(true);
    OutputStream os = con.getOutputStream();
    os.write(xmlData.getBytes("utf-8"));
    os.close();

}catch(Exception e){
      e.printStackTrace();
 }finally{
      con.disconnect();
 }

另一种选择是使用Apache 库。和代码:

 HttpPost httpPost = new HttpPost("URL");          
 StringEntity strEntity = new StringEntity(xmlData, "text/xml", "UTF-8");

 strEntity.setContentType("text/xml");  
 httpPost.setHeader("Content-Type","application/soap+xml;charset=UTF-8");
 httpPost.setEntity(strEntity);  
 HttpClient httpclient = new DefaultHttpClient();
 BasicHttpResponse httpResponse = (BasicHttpResponse) httpclient.execute(httpPost);

【讨论】:

  • 您好,请看问题。我想在方法中发布“xmlData”。如果您能提出相同的建议,谢谢。
  • 我的理解是你想发布 xml数据到特定的url。对吗?
  • 是的,实际上我想做类似的事情:HttpPost post = new HttpPost(api); List urlParameters = new ArrayList(); urlParameters.add(new BasicNameValuePair("html", html));我们可以做这样的事情吗?问题是我必须使用 con.setSSLSocketFactory(sslContext.getSocketFactory());在这里但不明白如何通过它发送'xmlData'参数。有什么建议吗?谢谢。
  • opps,您能否明确说明您为什么需要这样做,什么是 HttpPost 以及什么是 api ?也许你正在寻找的不是你需要的。
  • 嗨,请检查更新的问题。我附上了截图。这就是我想做的。你有什么想法?
猜你喜欢
  • 2012-11-25
  • 2014-01-25
  • 1970-01-01
  • 2018-05-17
  • 1970-01-01
  • 2020-10-31
  • 2015-01-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多