Socket 调用 WebService

在网上想找个实际的列子发现没有讲的特别好的!自己捣鼓了半天搞定了分享一下!

这个WebService的服务端是用Xfire生成的,客户端我用Socket实现调用的,功能很简单就是传进去2个参数然后打印出来。

我把服务器的接口代码贴一下:

package com.loadrunner;
//Generated by MyEclipse

public interface ILoadRunnerTest {
 
 public String example(String message,String message2);
 
}

下面我在贴一下

package com.client;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;

public class Test {

 /**
  * @param args
  * @throws IOException
  * @throws UnknownHostException
  */
 public static void main(String[] args) throws UnknownHostException, IOException {
  // TODO Auto-generated method stub
  Socket sc= new Socket("127.0.0.1",8080);
  OutputStream  os = sc.getOutputStream();
  InputStream   is= sc.getInputStream();
  String httpSend = "POST /LoadRunnerWebserviceT1/services/LoadRunnerTest HTTP/1.1\r\n" 
                           +"Content-Type:text/xml\r\n" 
                           +"Host:127.0.0.1:8080\r\n" 
                           +"Content-Length:380\r\n" 
                           +"SOAPACTION:\"urn:example\"\r\n" 
                           +"\r\n" 
                           +"<SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:SOAP-ENC=\"http://schemas.xmlsoap.org/soap/encoding/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">" 
                           +"<SOAP-ENV:Body>" 
                           +"<example xmlns=\"http://ws\">"
                           +"<message>"
                           +"AAAAAAAA"
                           +"</message>"
                           +"<message2>"
                           +"BBBBBBB"
                           +"</message2>"
                           +"</example>"
                           +" </SOAP-ENV:Body>" 
                           +"</SOAP-ENV:Envelope>";
  os.write(httpSend.getBytes());  
  os.flush();  
  InputStreamReader ireader = new InputStreamReader(is);  
          java.io.BufferedReader breader = new java.io.BufferedReader(ireader);  
           
         String responseLine = "";  
           
          while(!(responseLine = breader.readLine()).equals(""))  
         {  
             System.out.println(responseLine);  
          }  
            
          System.out.println("");  
           
          while(!(responseLine = breader.readLine()).equals(""))  
          {  
              System.out.println(responseLine);  
          } 

 }

}

 

 

服务器返回的结果:

HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
Content-Type: text/xml;charset=UTF-8
Transfer-Encoding: chunked
Date: Sun, 10 Jun 2012 04:13:58 GMT

14b
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><soap:Body><ns1:exampleResponse xmlns:ns1="http://loadrunner.com"><ns1:out>HelloLoadRunnerTestAAAAAAAA</ns1:out></ns1:exampleResponse></soap:Body></soap:Envelope>
0

 

 

特别说明下我标红字体的那些都不能搞错,搞错了就调用不成功:

POST /LoadRunnerWebserviceT1/services/LoadRunnerTest HTTP/1.1\r\n    POST或者GET都可以无所谓,后面这串/LoadR.........这个就是你的WebService的发布地址,后面不要挂?WSDL

Content-Type:text/xml\r\n"   这个不解释了!

Host:127.0.0.1:8080\r\n"     服务器地址
 
Content-Length:380\r\n"    这个是计算的+"<SOAP-ENV:Envelope xmlns:SOAP-ENV 之后的这些字节一共多少个 ,不要小于就行


SOAPACTION:\"urn:example\"\r\n"    你这次请求调用的服务的那个方法


+"\r\n"  必须换行,证明包头结束

这个动手试试就明白了。

 

想更深一步的了解可以装一个抓包工具,在你调用WebService接口的时候看看给服务端发的包是什么基本就清楚了!

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值