我的 Java 应用程序有一个非常有趣的问题。我正在尝试创建一个 SOAP 客户端,但我不想使用内置或任何其他外部库来使事情正常进行。我想了解它的实际工作原理并设置所有请求和标头信息。
该程序正在运行。我可以发送请求并获得响应。但是,当响应更大时,我在 xml 响应中得到一些“垃圾”,它会创建一个新行并出现字符串,例如:b50、51b、5a8、9ed 或 15b。这些字符串在响应中随机破坏了 xml 结构。
我的代码是:
package soapclient.client;
import java.io.*;
import java.net.*;
/**
*
* @author stom7
*/
public class Client {
private BufferedReader _reader;
private DataOutputStream _writer;
private Socket _socket;
public void connect() {
try
{
_socket = new Socket("**.***.***.***", 80);
}
catch (Exception ex) {
System.out.println(ex.getMessage());
return;
}
System.out.println("Connected successfully!");
try
{
_reader = new BufferedReader(new InputStreamReader(_socket.getInputStream()));
_writer = new DataOutputStream(_socket.getOutputStream());
}
catch (IOException ex) {
System.out.println(ex.getMessage());
return;
}
System.out.println("Streams created successfully!");
String content = "The soap request in xml format";
String request = "POST the_path HTTP/1.1\r\n"
+ "Host: the_host\r\n"
+ "Content-Type: application/soap+xml; charset=utf-8\r\n"
+ "Content-Length: " + content.length() + "\r\n"
+ "\r\n"
+ content;
try {
_writer.writeBytes(request);
_writer.flush();
String line;
while ((line = _reader.readLine()) != null) {
System.out.println(line);
}
}
catch(IOException ex) {
System.out.println(ex.getMessage());
}
try
{
_reader.close();
_writer.close();
_socket.close();
}
catch (Exception ex) {
System.out.println(ex.getMessage());
}
}
}
即使在第一行也有一个问题:
51b
<?xml version="1.0" encoding="UTF-8"?>
或者:
<xsd:element maxOccurs="1" minOccurs="1
b50
" name="in6" nillable="true" type="xsd:string"/>
谢谢!