`
lyw985
  • 浏览: 41205 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

用URLConnection传递文件参数,通过SOAP到Webservice

阅读更多
需求:使用URLConnection跟Webservice进行交互,其中有一个数据类型是base64Binary,对应到服务端就是byte[]。

PS:实际上就是文件处理

SOAP的请求文本格式
POST /MyService.asmx HTTP/1.1
Host: localhost
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "http://tempuri.org/upload"

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Body>
    <upload xmlns="http://tempuri.org/">
      <bs>base64Binary</bs>
    </upload>
  </soap:Body>
</soap:Envelope>


Server端
       public string upload(byte[] bs){
            return "123";//囧
       }


客户端
sun.misc.BASE64Encoder base64Encoder = new BASE64Encoder();
StringBuilder sb = new StringBuilder();
sb.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
sb
		.append("<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">");
sb.append("  <soap:Body>");
sb.append("    <upload xmlns=\"http://tempuri.org/\">");
sb.append("      <bs>");
byte[] bs=FileUtil.getByteArray(new File(""));//取得某个文本的byte数组,具体不写
sb.append(base64Encoder.encode(bs));
sb.append("</bs>");
sb.append("    </upload>");
sb.append("  </soap:Body>");
sb.append("</soap:Envelope>");
URL u = new URL(
		"http://localhost:4638/MyService.asmx");
HttpURLConnection conn = (HttpURLConnection) u.openConnection();
conn.setConnectTimeout(60000);
conn.setReadTimeout(60000);
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setUseCaches(false);
conn.setDefaultUseCaches(false);
conn.setRequestProperty("Content-Type", "text/xml; charset=utf-8");
conn.setRequestProperty("Content-Length", String.valueOf(sb.toString().length()));
conn.setRequestProperty("SOAPAction", "http://tempuri.org/upload");
conn.setRequestMethod("POST");

OutputStream output = conn.getOutputStream();
if (null != sb) {
	byte[] b = sb.toString().getBytes("utf-8");
	output.write(b, 0, b.length);
}
output.flush();
output.close();

InputStream input = conn.getInputStream();
int c = -1;
while (-1 != (c = input.read())) {
	sb.append((char) c);
}
input.close();
String result = sb.toString().replaceAll("&lt;", "<").replaceAll("&gt;", ">").replaceAll(
		"&quot;", "\"");
System.out.println(result);


中间的尝试过程:
由于一部分是String,一部分是byte数组,所以很是困扰
如果使用new String(byte[] bs)添加条件,会报这个错误
Caused by: com.ctc.wstx.exc.WstxUnexpectedCharException: Illegal character ((CTRL-CHAR, code 17))


如果遍历byte数组,添加每个byte,会报这个错误
java.io.IOException: Invalid header signature; read 0x9EF7F6BDD73DCEE3, expected 0xE11AB1A1E011CFD0

PS:后面的数据不是唯一的,不过就是这样格式的错误


最后发现,byte数组传递时需要转成Base64数据格式
例子如下
normal:This is a Test String
Base64:VGhpcyBpcyBhIFRlc3QgU3RyaW5n


给后人谋求福利~
分享到:
评论
1 楼 shixiuteng 2012-08-24  
我想知道服务端怎么写的。

相关推荐

    基于Servlet发送、接收SOAP消息的java源程序

    使用SOAP UI等工具测试服务的可用性和功能,确保消息的正确传递。 通过以上步骤,你可以成功地使用Servlet实现SOAP消息的发送与接收。理解并熟练运用这些知识,将使你在Web服务开发中更加得心应手。

    Java调用WebService(客户端)

    例如,可以使用`URLEncoder.encode()`对参数进行编码,然后将编码后的字符串写入到OutputStream中。这种方式同样返回一个简单的XML文档。 3. **HttpSoap方式**: HttpSoap方式使用SOAP(Simple Object Access ...

    java调用WebService(客户端)宣贯.pdf

    HttpGet方式通过HTTP GET请求将方法参数附加到URL的查询字符串中。例如,`http://example.com/service?param1=value1&param2=value2`。返回的数据通常作为简单的XML文档,不包含`&lt;soap:Envelope&gt;`标签。在Java中,你...

    java调用WebService(客户端)分享.pdf

    它首先使用`URLEncoder`对参数进行编码,然后构建URL,接着通过`URL`和`InputStreamReader`读取响应。 2. **HTTP POST**: - HTTP POST方法将参数放在HTTP请求的主体中,不受URL长度限制,适合传递大量数据。 - `...

    java调用WebService(客户端)[归类].pdf

    Java代码通常会创建一个`URLConnection`,然后通过`getOutputStream()`获取输出流,使用`OutputStreamWriter`写入参数。在提供的代码片段中,`post()`方法展示了如何实现HttpPost调用。 3. **HttpSoap调用** ...

    java调用WebService(客户端)整理.pdf

    在这个例子中,我们创建了一个`JaxWsDynamicClientFactory`实例,然后使用它创建了一个`Client`对象来调用指定URL的SOAP服务方法`getSupportCity`,并传递了参数"福建"。 在实际应用中,可能还需要处理异常、配置...

    java核心技术

    - **SOAP_教程.pdf**:简单对象访问协议(SOAP)是一种基于XML的消息传递协议,常用于Web服务。SOAP消息包含了方法调用、参数、返回值和错误信息,可以通过HTTP、SMTP等多种传输协议发送。 - **WebService描述语言...

Global site tag (gtag.js) - Google Analytics