gpt4 book ai didi

java - Java访问Web服务时URL和URI的区别

转载 作者:太空宇宙 更新时间:2023-11-04 06:50:02 25 4
gpt4 key购买 nike

我正在访问网络服务。下面是我的 PHP 代码,它运行得很好:

class fNewRequest {
function fNewReq($pTK, $pVID)
{
$this->pTK= $pTK;
$this->pVID= $pVID;
}
}

/* Initialize webservice with your WSDL */
$client = new SoapClient("http://**.**.**.*/**/**/***/webservice.asmx?WSDL");

/* Set your parameters for the request */
$params = array(
"pTK" => "asa",
"pVID" => "1134",
);

/* Invoke webservice method with your parameters, in this case: Function1 */
$response = $client->__soapCall("fNewRequests", array($params));

/* Print webservice response */
var_dump($response);

print_r($params);

我现在正在尝试用 Java 做同样的事情。下面是我的代码:

import java.util.*;
import java.io.*;
import java.net.URLConnection;
import java.net.MalformedURLException;

import javax.xml.soap.*;
import javax.xml.transform.*;
import javax.xml.transform.stream.*;


public class ws1
{
public static void ws1()
{
try
{
SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance();
SOAPConnection soapConnection = soapConnectionFactory.createConnection();
String url = "http://**.**.**.*/**/**/***/webservice.asmx?WSDL";
SOAPMessage soapResponse = soapConnection.call(createSOAPRequest(), url);
printSOAPResponse(soapResponse);
}
catch (Exception e)
{
e.printStackTrace();
System.err.println(e.toString());
}
}

private static SOAPMessage createSOAPRequest() throws Exception {
MessageFactory messageFactory = MessageFactory.newInstance();
SOAPMessage soapMessage = messageFactory.createMessage();
SOAPPart soapPart = soapMessage.getSOAPPart();
String serverURI = "http://**.**.**.*/**/**/***/webservice.asmx?WSDL";

SOAPEnvelope envelope = soapPart.getEnvelope();
envelope.addNamespaceDeclaration("wsdl", serverURI);

SOAPBody soapBody1 = envelope.getBody();
SOAPElement soapBodyElemX = soapBody1.addChildElement("fNewRequests", "wsdl");

SOAPElement soapBodyElem1 = soapBodyElemX.addChildElement("pTK", "wsdl");
soapBodyElem1.addTextNode("asa");
SOAPElement soapBodyElem2 = soapBodyElemX.addChildElement("pVID", "wsdl");
soapBodyElem2.addTextNode("1134");

MimeHeaders headers = soapMessage.getMimeHeaders();
headers.addHeader("performRecharge", serverURI + "performRecharge");
System.out.println("");
soapMessage.saveChanges();

System.out.print("Request SOAP Message = ");
soapMessage.writeTo(System.out);
System.out.println();
return soapMessage;
}

private static void printSOAPResponse(SOAPMessage soapResponse) throws Exception {
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
Source sourceContent = soapResponse.getSOAPPart().getContent();
System.out.print("\nResponse SOAP Message = ");
StreamResult result = new StreamResult(System.out);
transformer.transform(sourceContent, result);
}

public static void main(String[] args) {
ws1();
}
}

我得到了这个:

Response SOAP Message = <?xml version="1.0" encoding="UTF-8"?><soap:Envelope  xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
<soap:Fault>
<faultcode>soap:Client</faultcode>
<faultstring>System.Web.Services.Protocols.SoapException: Server did not recognize the value of HTTP Header SOAPAction:
at System.Web.Services.Protocols.Soap11ServerProtocolHelper.RouteRequest()
at System.Web.Services.Protocols.SoapServerProtocol.RouteRequest(SoapServerMessage message)
at System.Web.Services.Protocols.SoapServerProtocol.Initialize()
at System.Web.Services.Protocols.ServerProtocolFactory.Create(Type type, HttpContext context, HttpRequest request, HttpResponse response, Boolean&amp; abortProcessing)
</faultstring>
<detail/>
</soap:Fault>
</soap:Body>
</soap:Envelope>

我不知道该为 URL 和 URI 设置什么值?请问有什么帮助吗?

最佳答案

该 URL 是您将请求发送到的位置。 URI 是正文消息必须具有的 XML 命名空间(也可以是 an URL is an URI 以来的 URL)。

您还必须确保发送 SOAPAction HTTP header (我在错误中看到服务器对此有提示)。

不知道您的 WSDL 中有什么内容,因此我将使用在线 Web 服务进行解释,可以在此处找到其描述:http://www.webservicex.net/ConvertTemperature.asmx?WSDL .

为了调用此 Web 服务,我需要向 http://www.webservicex.net/ConvertTemperature.asmx 发送请求。从 WSDL,我知道请求的结构,并且知道元素要使用什么命名空间:http://www.webserviceX.NET/。另外,从 WSDL 我知道要发送的 SOAPAction:http://www.webserviceX.NET/ConvertTemp

代码如下:

package com.test;

import javax.xml.soap.MessageFactory;
import javax.xml.soap.MimeHeaders;
import javax.xml.soap.SOAPBody;
import javax.xml.soap.SOAPConnection;
import javax.xml.soap.SOAPConnectionFactory;
import javax.xml.soap.SOAPElement;
import javax.xml.soap.SOAPEnvelope;
import javax.xml.soap.SOAPMessage;

public class Main {
public static void main(String... args) throws Exception {
SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance();
SOAPConnection soapConnection = soapConnectionFactory.createConnection();

String url = "http://www.webservicex.net/ConvertTemperature.asmx";
SOAPMessage request = Main.createSOAPRequest();

System.out.print("Request: ");
request.writeTo(System.out);

SOAPMessage response = soapConnection.call(request, url);

System.out.print("\nResponse: ");
response.writeTo(System.out);
}

private static SOAPMessage createSOAPRequest() throws Exception {
SOAPMessage soapMessage = MessageFactory.newInstance().createMessage();

MimeHeaders headers = soapMessage.getMimeHeaders();
headers.addHeader("SOAPAction", "http://www.webserviceX.NET/ConvertTemp");

SOAPEnvelope envelope = soapMessage.getSOAPPart().getEnvelope();

SOAPBody soapBody = envelope.getBody();
soapBody.addNamespaceDeclaration("prefix", "http://www.webserviceX.NET/");

SOAPElement convertTempElement = soapBody.addChildElement("ConvertTemp", "prefix");

SOAPElement temperatureElement = convertTempElement.addChildElement("Temperature", "prefix");
temperatureElement.addTextNode("100");

SOAPElement fromUnitElement = convertTempElement.addChildElement("FromUnit", "prefix");
fromUnitElement.addTextNode("degreeCelsius");

SOAPElement toUnitElement = convertTempElement.addChildElement("ToUnit", "prefix");
toUnitElement.addTextNode("degreeFahrenheit");

soapMessage.saveChanges();
return soapMessage;
}
}

程序发送此请求:

<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
<SOAP-ENV:Header/>
<SOAP-ENV:Body xmlns:prefix="http://www.webserviceX.NET/">
<prefix:ConvertTemp>
<prefix:Temperature>100</prefix:Temperature>
<prefix:FromUnit>degreeCelsius</prefix:FromUnit>
<prefix:ToUnit>degreeFahrenheit</prefix:ToUnit>
</prefix:ConvertTemp>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>

并得到此响应:

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
<ConvertTempResponse xmlns="http://www.webserviceX.NET/">
<ConvertTempResult>212</ConvertTempResult>
</ConvertTempResponse>
</soap:Body>
</soap:Envelope>

将此示例与您拥有的示例进行比较,您就会找出要使用的值。

关于java - Java访问Web服务时URL和URI的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23433688/

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