【问题标题】:Creating SOAP request manually - server receives null string as an argument手动创建 SOAP 请求 - 服务器接收空字符串作为参数
【发布时间】:2012-11-19 10:11:15
【问题描述】:

所以,问题是,我已经(在 localhost 上)设置了一个 Java Webservice 服务器,它使用 Endpoint.publish 发布服务,它是 WSDL,以及尝试发送手写 SOAP 请求的 iOS 6 项目本地服务器。

我有一个“getCostForName”函数,它接收字符串并返回 int。 在 iOS 端,这就是我正在做的事情:

{
    NSString *soapMessage = @"<?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/\" xmlns=\"http://endpoints/\" >"
    "<soap:Body>"
    "<getCostForName>"
    "<name>Gdansk</name>"
    "</getCostForName>"
    "</soap:Body>"
    "</soap:Envelope>";

    NSURL *url = [NSURL URLWithString:@"http://localhost:9999/ws/wycieczka/"];
    NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];
    NSString *msgLength = [NSString stringWithFormat:@"%d", [soapMessage length]];

    [theRequest addValue: @"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
    [theRequest addValue: @"http://localhost:9999/ws/wycieczka/" forHTTPHeaderField:@"SOAPAction"];
    [theRequest addValue: msgLength forHTTPHeaderField:@"Content-Length"];
    [theRequest setHTTPMethod:@"POST"];
    [theRequest setHTTPBody: [soapMessage dataUsingEncoding:NSUTF8StringEncoding]];

    NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];

    if( theConnection )
    {
        webData = [[NSMutableData data] retain];
    }
    else
    {
        NSLog(@"theConnection is NULL");
    }

}

-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    [webData setLength: 0];
}
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    [webData appendData:data];
}
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
    NSLog(@"ERROR with theConenction");
    [connection release];
    [webData release];
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    NSLog(@"DONE. Received Bytes: %d", [webData length]);
    NSString *theXML = [[NSString alloc] initWithBytes: [webData mutableBytes] length:[webData length] encoding:NSUTF8StringEncoding];
    NSLog(@"%@",theXML);
    [theXML release];

}

在 Java 端,它看起来像这样:

    @Override
    public Integer getCostForName(String name) {
        System.out.println("getCostForName received:" + name);
        //Calculate the cost for a given name...
    }

webservice接口由以下注解定义:

@WebService
@SOAPBinding(style=SOAPBinding.Style.RPC)

使用以下端点发布方法托管 Web 服务:

Endpoint.publish("http://localhost:9999/ws/wycieczka", new WycieczkaServiceImpl());

在 Java 方面,这是我得到的:

Nov 19, 2012 11:08:52 AM com.sun.xml.internal.ws.transport.http.HttpAdapter fixQuotesAroundSoapAction
getCostForName received:null
WARNING: Received WS-I BP non-conformant Unquoted SoapAction HTTP header: http://localhost:9999/ws/wycieczka/

因此,如您所见,服务器接收到空字符串。而且我不知道为什么我不能通过网络服务传输一个字符串:(那么,有人可以告诉我我做错了什么吗?

【问题讨论】:

  • 除非万不得已,否则我不会使用 SOAP,因为它实在是太复杂了。为什么不使用 Apache Jackrabbit (jackrabbit.apache.org) 之类的东西创建一个 RESTful Web 服务?
  • 无法更改服务器。它是 SOAP,它会一直是 SOAP。完全不可能过渡到 RESTful,因为决定我们将使用哪种方法不是我的事。我的同事选择了那个,他不可能改变主意,这就是我坚持使用 SOAP 的原因。

标签: java string web-services soap ios6


【解决方案1】:

我认为这可能是因为您在请求中缺少 SOAP 标头,尽管我确实会避免通过字符串连接来执行此操作。 SOAP 是一个非常复杂的协议,它需要适当的客户端实现。试试这个...

How to access SOAP services from iPhone

【讨论】:

  • 我在请求中添加了一个
    ,但结果仍然相同。我尝试使用 sudzc,但是我遇到了完全相同的问题。我会尝试使用 wsdl2objc,但是我不知道这是否可行:(
  • 不,wsdl2OBJC 也不起作用。由于没有足够的空间,这就是我所拥有的:粘贴到 pastebin:link
  • 也许这有助于确认 SOAP 服务首先与 SOAP UI (sourceforge.net/projects/soapui/files) 之类的东西一起工作
  • SOAP 服务本身就可以工作。我有一个典型的 HelloWorld 服务,它什么都不接收并返回字符串:“hello world!”我能够通过 sudzc 手动成功接收该字符串
【解决方案2】:

我在使用 ksopa2 ver3 时遇到了同样的问题,但我没有使用手写 SOAP,我使用的是从 www.wsdl2code.com 自动生成的分类。有一条线

soapEnvelope.dotNet = true;

在为我的 EndPoint 生成的 java 文件中。我将其更改为 false,之后效果很好。

阿基尔

【讨论】:

    【解决方案3】:

    根据 SOAP 1.1 规范的第 6.1.1 节,SOAPAction HTTP 标头必须在 URI 引用(如果存在)周围有双引号。

    这就是您收到此警告的原因:

    WARNING: Received WS-I BP non-conformant Unquoted SoapAction HTTP header: http://localhost:9999/ws/wycieczka/
    

    应该是:

     [theRequest addValue: @"\"http://localhost:9999/ws/wycieczka/\"" forHTTPHeaderField:@"SOAPAction"];
    

    【讨论】:

      猜你喜欢
      • 2011-06-24
      • 2012-05-03
      • 1970-01-01
      • 2016-10-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-13
      • 1970-01-01
      相关资源
      最近更新 更多