簡體   English   中英

如何使用 HttpURLConnection 在 Java 中包含 SOAP 請求的標頭信息

[英]How to include header information for a SOAP request in Java using HttpURLConnection

我需要引入這些頭元素:Enable MTOM、Force MTOM、WSS-PasswordType: PasswordDigest、WSS TimeToLive: 50 和使用用戶和密碼的基本身份驗證,我需要在這個soap請求中附加一個文檔。 我搜索了有關 HttpURLConnection 的文檔,但找不到任何內容。

我目前的代碼:

import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import org.apache.commons.lang.StringUtils;

public class Send_XML_Post_Request_1 {
    public static void main(String[] args) throws MalformedURLException, IOException {
        String urlString = "https://ws1.soc.com.br/WSSoc/services/UploadArquivosWs?wsdl";
        URL urlForInfWebSvc = new URL(urlString);
        URLConnection UrlConnInfWebSvc = urlForInfWebSvc.openConnection();
        HttpURLConnection httpUrlConnInfWebSvc = (HttpURLConnection) UrlConnInfWebSvc;
        httpUrlConnInfWebSvc.setDoOutput(true);
        httpUrlConnInfWebSvc.setDoInput(true);
        httpUrlConnInfWebSvc.setAllowUserInteraction(true);
        httpUrlConnInfWebSvc.setRequestMethod("POST");
        httpUrlConnInfWebSvc.setRequestProperty("Content-Type","application/soap+xml; charset=utf-8");
        OutputStreamWriter infWebSvcReqWriter = new OutputStreamWriter(httpUrlConnInfWebSvc.getOutputStream());
        String infWebSvcRequestMessage = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:ser=\"http://services.soc.age.com/\"> <soapenv:Header/> <soapenv:Body> <ser:uploadArquivo> <arg0> <arquivo></arquivo> <classificacao>FICHA_CLINICA_BRANCO</classificacao> <codigoEmpresa>297819</codigoEmpresa> <codigoFuncionario>3866</codigoFuncionario> <codigoSequencialFicha>133382762</codigoSequencialFicha> <extensaoArquivo>PDF</extensaoArquivo> <identificacaoVo> <chaveAcesso>1e93a60985ff95e</chaveAcesso> <codigoEmpresaPrincipal>62168</codigoEmpresaPrincipal> <codigoResponsavel>17863</codigoResponsavel> <homologacao>false</homologacao> <codigoUsuario>422450</codigoUsuario> </identificacaoVo> <nomeArquivo>TESTE</nomeArquivo> <sobreescreveArquivo>false</sobreescreveArquivo> </arg0> </ser:uploadArquivo> </soapenv:Body> </soapenv:Envelope>";
        infWebSvcReqWriter.write(infWebSvcRequestMessage);
        infWebSvcReqWriter.flush();
        BufferedReader infWebSvcReplyReader = new BufferedReader(new InputStreamReader(httpUrlConnInfWebSvc.getInputStream()));
        String line;
        String RetornoWS = "";
        while ((line = infWebSvcReplyReader.readLine()) != null) {
            RetornoWS = RetornoWS.concat(line);
            }
        infWebSvcReqWriter.close();
        infWebSvcReplyReader.close();
        httpUrlConnInfWebSvc.disconnect();
        String Resposta = StringUtils.substringBetween(RetornoWS,"&lt;return>","&lt;/return>");
        System.out.println(Resposta);
        }
}```

您已經使用設置了一個標題屬性

httpUrlConnInfWebSvc.setRequestProperty("Content-Type","application/soap+xml; charset=utf-8");

類似的方式你可以通過

httpUrlConnInfWebSvc.setRequestProperty("Enable MTOM","true");
httpUrlConnInfWebSvc.setRequestProperty("Force MTOM","true");
httpUrlConnInfWebSvc.setRequestProperty("WSS-PasswordType","PasswordDigest");
httpUrlConnInfWebSvc.setRequestProperty("WSS TimeToLive","50");

對於基本身份驗證標頭,請設置授權

String credentials = "user:password";
String basicAuth = "Basic " + new String(Base64.getEncoder().encode(credentials.getBytes()));    
httpUrlConnInfWebSvc.setRequestProperty ("Authorization", basicAuth);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM