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

标签 java string web-services soap ios6

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

我有一个“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...
    }

Web服务接口(interface)由以下注释定义:

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

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

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 header ,尽管我确实会避免通过字符串连接来执行此操作。 SOAP 是一个非常复杂的协议(protocol),它需要适当的客户端实现。试试这个...

How to access SOAP services from iPhone

关于java - 手动创建 SOAP 请求 - 服务器接收空字符串作为参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13451627/

相关文章:

java - 使用 Jackson 将 json 映射到 java 对象时汇总所有错误

php - 如何将 Camel Case 解析为人类可读的字符串?

java - log4j 在带有 Spring 的 JAX-WS 中无法正常工作

java - jmeter中soap web服务的xpath断言

Java:尝试使用 Sieve 计算素数时出现 ArrayOutofBounds 异常

java - 2 具有红色背景的java中矩形的平滑圆角

java - Android:将现有代码导入 Eclipse

java - 包装类,通过构造函数中的字符串参数创建对象时的功能差异?

swift - Swift 如何哈希字符串?

java - 使用 CXF Rest 客户端解析响应实体