首页 文章

手动创建SOAP请求 - 服务器接收空字符串作为参数

提问于
浏览
0

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

我有一个“getCostForName”函数接收String并返回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)

使用以下 endpoints 发布方法托管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/

因此,正如您所看到的,服务器收到空字符串 . 我无法弄清楚为什么我不能通过webservice传输一个字符串:(所以,任何人,请,告诉我,我做错了什么?

3 回答

  • 1

    我认为这可能是因为你在请求中缺少SOAP头,但我真的会避免通过String连接来做这件事 . SOAP是一个非常复杂的协议,它需要一个适当的客户端实现 . 试试这个...

    How to access SOAP services from iPhone

  • 1

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

    soapEnvelope.dotNet = true;

    在为我的EndPoint生成的java文件中 . 我把它改为假,之后它运作良好 .

    阿基洛夫

  • 0

    根据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"];
    

相关问题