开发者社区> 问答> 正文

如何使用HttpURLConnection在Java中包含SOAP请求的标头信息

我需要介绍以下标头元素:启用MTOM,强制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,"<return>","</return>");
        System.out.println(Resposta);
        }
}```


问题来源:Stack Overflow

展开
收起
montos 2020-03-28 09:11:06 822 0
1 条回答
写回答
取消 提交回答
  • 您已经使用设置了一个标头属性

    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);
    

    回答来源:Stack Overflow

    2020-03-28 09:11:33
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
Spring Cloud Alibaba - 重新定义 Java Cloud-Native 立即下载
The Reactive Cloud Native Arch 立即下载
JAVA开发手册1.5.0 立即下载