gpt4 book ai didi

java - 如何使用 HttpURLConnection 在 Java 中包含 SOAP 请求的 header 信息

转载 作者:行者123 更新时间:2023-12-01 17:59:46 26 4
gpt4 key购买 nike

我需要引入这些 header 元素:启用 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,"&lt;return>","&lt;/return>");
System.out.println(Resposta);
}
}```

最佳答案

您已经使用设置了一个 header 属性

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

对于基本身份验证 header ,设置授权

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

关于java - 如何使用 HttpURLConnection 在 Java 中包含 SOAP 请求的 header 信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60657826/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com