namespaceURI
时间: 2024-02-03 09:03:54 浏览: 16
namespaceURI (命名空间URI) 是一个用于标识 XML 元素命名空间的字符串。在 XML 文档中,命名空间被用于区分具有相同名称的元素,以避免命名冲突。命名空间URI 可以是任何有效的 URI(Uniform Resource Identifier),通常是一个 URL(Uniform Resource Locator)或 URN(Uniform Resource Name)。
在 XML 中,可以使用 xmlns 属性来指定命名空间URI。例如:
```xml
<root xmlns:ns="http://example.com">
<ns:element>Some content</ns:element>
</root>
```
在上面的例子中,`xmlns:ns="http://example.com"` 定义了一个名为 ns 的命名空间,它的 URI 是 "http://example.com"。因此,`<ns:element>` 元素属于该命名空间,它的完整名称是 "ns:element"。
相关问题
在下面这段样例代码中我如何改造成调用https的接口··· //样例代码,获取手机号码归属地 public static void getMobileCodeInfo() throws ServiceException, RemoteException { Service service = new Service(); Call call = (Call) service.createCall(); // wsdl完整地址 call.setTargetEndpointAddress("http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx?wsdl"); /** * 设置方法名 * new QName(String namespaceURI, String localPart) namespaceURI即为wsdl中的targetNamespace, localPart即为接口名 */ call.setOperationName(new QName("http://WebXml.com.cn/", "getMobileCodeInfo")); /** * 添加参数 * addParameter方法的参数包括:参数名(namespace+参数名)、参数类型、ParameterMode(入参即为IN) */ call.addParameter(new QName("http://WebXml.com.cn/", "mobileCode"), XMLType.XSD_STRING, ParameterMode.IN); call.setUseSOAPAction(true); // SOAPActionURI格式为targetNamespace+方法名 call.setSOAPActionURI("http://WebXml.com.cn/getMobileCodeInfo"); // 指定返回值类型,为字符串 call.setReturnType(XMLType.XSD_STRING); call.setReturnClass(java.lang.String.class); String result = (String) call.invoke(new Object[]{"13999903152"}); System.out.println(result); }···
您可以按照以下步骤将上述样例代码改造为调用https的接口:
1. 将wsdl地址中的http替换成https,即将以下代码:
call.setTargetEndpointAddress("http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx?wsdl");
改为:
call.setTargetEndpointAddress("https://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx?wsdl");
2. 创建SSLContext实例,并设置信任管理器TrustManager来信任所有证书:
```java
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
//...
TrustManager[] trustAllCerts = new TrustManager[]{
new X509TrustManager() {
public X509Certificate[] getAcceptedIssuers() {
return null;
}
public void checkClientTrusted(X509Certificate[] certs, String authType) throws CertificateException {
}
public void checkServerTrusted(X509Certificate[] certs, String authType) throws CertificateException {
}
}
};
SSLContext sslContext = SSLContext.getInstance("TLS");
sslContext.init(null, trustAllCerts, new java.security.SecureRandom());
```
3. 设置HttpsURLConnection默认的SSLSocketFactory和HostnameVerifier:
```java
import javax.net.ssl.HttpsURLConnection;
//...
HttpsURLConnection.setDefaultSSLSocketFactory(sslContext.getSocketFactory());
HttpsURLConnection.setDefaultHostnameVerifier((hostname, sslSession) -> true);
```
4. 将调用方式改为使用HttpURLConnection,即将以下代码:
String result = (String) call.invoke(new Object[]{"13999903152"});
改为:
URL url = new URL("https://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", "text/xml;charset=UTF-8");
connection.setDoOutput(true);
String soapRequest = "<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">\n" +
" <soap:Body>\n" +
" <getMobileCodeInfo xmlns=\"http://WebXml.com.cn/\">\n" +
" <mobileCode>13999903152</mobileCode>\n" +
" <userID></userID>\n" +
" </getMobileCodeInfo>\n" +
" </soap:Body>\n" +
"</soap:Envelope>";
OutputStream outputStream = connection.getOutputStream();
outputStream.write(soapRequest.getBytes("UTF-8"));
String result = null;
try (BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"))) {
StringBuilder stringBuilder = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
stringBuilder.append(line);
}
result = stringBuilder.toString();
}
System.out.println(result);
```
请注意替换代码中的mobileCode参数值和soapRequest中的namespace和方法名。同时,为了保证代码的安全性,请在实际使用过程中,不要信任所有证书,可以使用合法的证书或者自己实现TrustManager来进行证书校验。
schemalocation
schemaLocation是XML文件中的一个属性,用于指定XML文件中命名空间的位置和对应的XML Schema文件的位置。它的格式为“namespaceURI schemaLocation”,其中namespaceURI是命名空间的URI,schemaLocation是对应的XML Schema文件的位置。schemaLocation属性可以出现在XML文件的根元素上,也可以出现在其他元素上,用于指定该元素的命名空间和对应的XML Schema文件的位置。
例如,在下面的XML文件中,根元素project的命名空间是"http://maven.apache.org/POM/4.0.0",对应的XML Schema文件的位置是"http://maven.apache.org/maven-v4_0_0.xsd"。
```xml
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
...
</project>
```
相关推荐
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)