简体   繁体   English

Java:如何发布此XML请求?

[英]Java: How to post this XML request?

i'm working with SOA using WebServices. 我正在使用WebServices处理SOA。 So i need to send a XML request to receive another XML with the response. 所以我需要发送一个XML请求来接收带有响应的另一个XML。

I create this class: 我创建这个类:

package com.ws.test;

import java.net.*;
import java.io.*;

public class SendXML {

    public static void main(String[] args) {

        try {
            String strSOAP = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";

            strSOAP += "<SOAP-ENV:Envelope ";

            strSOAP += " xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/";

            strSOAP += " xmlns:SOAP-ENC=\"http://schemas.xmlsoap.org/soap/encoding/";

            strSOAP += " xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance";

            strSOAP += " xmlns:xsd=\"http://www.w3.org/2001/XMLSchema";

            strSOAP += " xmlns:ns=\"urn:bacnet_ws\">";

            strSOAP += " <SOAP-ENV:Body SOAP-ENV:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\">";

            strSOAP += " <ns:getValue>";

            strSOAP += " <ns:options></ns:options>";

            strSOAP += " <ns:path>/.sysinfo/.vendor-name</ns:path>";

            strSOAP += " </ns:getValue>";

            strSOAP += " </SOAP-ENV:Body>";

            strSOAP += "</SOAP-ENV:Envelope>";


            //Create socket
            String hostname = "192.168.1.2";
            int port = 8080;
            InetAddress addr = InetAddress.getByName(hostname);
            Socket sock = new Socket(addr, port);

            //Send header
            String path = "/rcx-ws/rcx";
            BufferedWriter wr = new BufferedWriter(new OutputStreamWriter(sock.getOutputStream(), "UTF-8"));
            // You can use "UTF8" for compatibility with the Microsoft virtual machine.
            wr.write("POST " + path + " HTTP/1.0\r\n");
            wr.write("Host: 192.168.1.2\r\n");
            wr.write("Content-Length: " + strSOAP.length() + "\r\n");
            wr.write("Content-Type: text/xml; charset=\"utf-8\"\r\n");
            wr.write("\r\n");

            //Send data
            wr.write(strSOAP);
            wr.flush();

            // Response
            BufferedReader rd = new BufferedReader(new InputStreamReader(sock.getInputStream()));
            String line;
            while ((line = rd.readLine()) != null) {

                System.out.println(line);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

But this is not right because i receive this message error: 但这不对,因为我收到此消息错误:

HTTP/1.1 500 Internal Server Error Server: gSOAP/2.7 Content-Type: text/xml; HTTP / 1.1 500内部服务器错误服务器:gSOAP / 2.7内容类型:text / xml; charset=utf-8 Content-Length: 570 Connection: close charset = utf-8内容长度:570连接:关闭

SOAP-ENV:VersionMismatchSOAP version mismatch or invalid SOAP message SOAP-ENV:VersionMismatchSOAP版本不匹配或无效的SOAP消息

I need to send this parameter to the another system : 我需要将此参数发送到另一个系统:

strSOAP += " <ns:getValue>";

strSOAP += " <ns:options></ns:options>";

strSOAP += " <ns:path>/.sysinfo/.vendor-name</ns:path>";

strSOAP += " </ns:getValue>";

How i could do that in Java ? 我怎么能用Java做到这一点?

Best regards, Valter Henrique. 此致,Valter Henrique。

Good lord dude. 好主人。 Not to be overly sarcastic, but haven't you heard of JAX-WS ? 不要过于讽刺,但你没有听说过JAX-WS吗? JAX-RS ? JAX-RS Or even the humble, but reliable JAX-RPC ? 甚至是简陋但可靠的JAX-RPC How could you be working on web-service based SOA if you are actually trying to build a web service request by hand like that, in such a paleolithic way. 如果你真的试图像这样以旧石器时代的方式手工构建一个Web服务请求,你怎么能在基于Web服务的SOA上工作呢?

I mean, c'mon, I know we shouldn't tell people to 'google it', but seriously, did it ever occur to you to do a google on 'web service call example' before trying that coding madness? 我的意思是,来吧,我知道我们不应该告诉别人'google it',但是说真的,在尝试编码疯狂之前,你曾经在'网络服务电话示例'做谷歌吗? This is 2011, not 1994-95 when most people didn't know what a web search was. 这是2011年,而不是1994-95,当时大多数人都不知道网络搜索是什么。

I suggest you start with the Java EE 5 JAX-WS tutorial as there is much for you to learn. 我建议你从Java EE 5 JAX-WS教程开始,因为你需要学习很多东西。 Good luck. 祝好运。

If you insist on using sockets to transmit data, you'll have to look at the SOAP specifications . 如果您坚持使用套接字传输数据,则必须查看SOAP规范 For example, SOAP 1.1 requires the SOAPAction HTTP header . 例如,SOAP 1.1需要SOAPAction HTTP标头 Use a tool like soapUI to check the validity of requests before you start writing code. 在开始编写代码之前,使用soapUI之类的工具检查请求的有效性。

In general, you'd be better off using an existing client API (of which there are a few to choose). 通常,您最好使用现有的客户端API(其中有一些可供选择)。 For example, you can easily use hand-crafted soap requests with JAX-WS . 例如,您可以使用JAX-WS轻松使用手工制作的soap请求

A couple of notes: 几个笔记:


The code leaks resources. 代码泄漏了资源。 Close your streams. 关闭你的溪流。 See the try/finally pattern . 请参阅try / finally模式


BufferedWriter wr = new BufferedWriter(
                     new OutputStreamWriter(sock.getOutputStream(), "UTF-8"));
//...
wr.write("Content-Length: " + strSOAP.length() + "\r\n");

This would not be safe for code points above U+007F as String.length() returns the number of code units in UTF-16. 对于U + 007F以上的代码点,这是不安全的,因为String.length()返回UTF-16中的代码单元数。 Content-Length should contain the length in byte s; Content-Length应包含以byte单位的长度; not Java char s. 不是Java char

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM