使用Java Web Service的Objective C SOAP请求 [英] Objective C SOAP Request with Java Web Service

查看:100
本文介绍了使用Java Web Service的Objective C SOAP请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Java中有一个简单的Web服务:

I have a simple Web service in Java:

@WebService(serviceName = "Catalogo_V1")
public class Catalogo_V1 {

        /** This is a sample web service operation */
        @WebMethod(operationName = "hello")
        public String hello(@WebParam(name = "name") String txt) 
        {
            System.out.println("kkk"+txt);
            if(txt != null)
            {
                txt= txt +"www";
            }

            return "Hello " + txt + " !";
        }

    }

我在这里做了一个测试Web服务:(http:// localhost:8080 / WSServer / Catalogo_V1?Tester)

I have done a Test in this web service: (http://localhost:8080/WSServer/Catalogo_V1?Tester)

返回方法

java.lang.String : "Hello davidwww !"

SOAP Request

<?xml version="1.0" encoding="UTF-8"?>
<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
    <S:Header/>
    <S:Body>
        <ns2:hello xmlns:ns2="http://org/">
            <name>david</name>
        </ns2:hello>
    </S:Body>
</S:Envelope>

SOAP响应

<?xml version="1.0" encoding="UTF-8"?>
<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
    <S:Body>
        <ns2:helloResponse xmlns:ns2="http://org/">
            <return>Hello davidwww !</return>
        </ns2:helloResponse>
    </S:Body>
</S:Envelope>

好的,之后我在目标C中完成了这个请求:

Ok, after that i have done this request in Objective C:

ConfigViewController.h

@interface ConfigViewController : UITableViewController 
{
NSMutableData *webData;
    NSXMLParser *xmlParser;
    NSString *finaldata;
    NSString *convertToStringData;
    NSMutableString *nodeContent;

}
-(IBAction) buttonPressed: (id) sender;


@end

ConfigViewController.m

#import "ConfigViewController.h"

@implementation ConfigViewController

...

- (IBAction)buttonPressed:(id)sender 
{

nodeContent = [[NSMutableString alloc]init];

    NSString *soapFormat = [NSString stringWithFormat:
                            @"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
                            "<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/\">\n"
                            "<soap:Body>\n"
                            "<ns2:hello xmlns:ns2=\"http://org/\" />\n"
                            "<name>david</name>\n"
                            "</ns2:hello>\n"
                            "</soap:Body>\n"
                            "</soap:Envelope>\n"];



    NSLog(@"The request format is: \n%@",soapFormat);

    NSURL *locationOfWebService = [NSURL URLWithString:@"http://localhost:8080/WSServer/Catalogo_V1?wsdl"];

    NSLog(@"web url = %@",locationOfWebService);

    NSMutableURLRequest *theRequest = [[NSMutableURLRequest alloc]initWithURL:locationOfWebService];

    NSString *msgLength = [NSString stringWithFormat:@"%d",[soapFormat length]];


    [theRequest addValue:@"text/xml" forHTTPHeaderField:@"Content-Type"];
    [theRequest addValue:@"http://localhost:8080/WSServer/Catalogo_V1?wsdl" forHTTPHeaderField:@"SOAPAction"];
    [theRequest addValue:msgLength forHTTPHeaderField:@"Content-Length"];
    [theRequest setHTTPMethod:@"POST"];
    //the below encoding is used to send data over the net
    [theRequest setHTTPBody:[soapFormat dataUsingEncoding:NSUTF8StringEncoding]];


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

    if (connect) {
        webData = [[NSMutableData alloc]init];
    }
    else {
        NSLog(@"No Connection established");
    }
}

-(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];
}

-(BOOL)connection:(NSURLConnection *)connection canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace
{
    return YES;
}

-(void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
{
    NSURLCredential *credential = [NSURLCredential credentialWithUser:@"username" password:@"password" persistence:NSURLCredentialPersistenceForSession];
    [[challenge sender] useCredential:credential forAuthenticationChallenge:challenge];
}


-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    NSLog(@"DONE. Received Bytes: %d \n", [webData length]);
    NSString *theXML = [[NSString alloc] initWithBytes: [webData mutableBytes] length:[webData length] encoding:NSUTF8StringEncoding];
    NSLog(@"\n%@",theXML);
 [connection release];
}

...

@end

我的控制台显示:

    2011-09-02 10:17:32.748 Catalogo-V1[1131:207] The request format is: 
    <?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>
    <ns2:hello xmlns:ns2="http://org/" />
    <name>david</name>
    </ns2:hello>
    </soap:Body>
    </soap:Envelope>
    2011-09-02 10:17:32.749 Catalogo-V1[1131:207] web url = http://localhost:8080/WSServer/Catalogo_V1?wsdl
    2011-09-02 10:17:32.757 Catalogo-V1[1131:207] DONE. Received Bytes: 224 
    2011-09-02 10:17:32.757 Catalogo-V1[1131:207] 
    <?xml version='1.0' encoding='UTF-8'?>
    <S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">        
<S:Body> 
<ns2:helloResponse xmlns:ns2="http://org/">
<return>Hello null !</return>
</ns2:helloResponse>
</S:Body>
</S:Envelope>


推荐答案

根据你在控制台上显示的内容,您制定的XML请求无效。你有

According to what you've shown from your console, the XML request you formulated is invalid. You have

<ns2:hello xmlns:ns2="http://org/" />
    <name>david</name>
</ns2:hello>

但你应该(注意删除无效的XML标签终止):

But you should have (note the removal of the invalid XML tag termination):

<ns2:hello xmlns:ns2="http://org/>
    <name>david</name>
</ns2:hello>

另请注意,您的工作样品申请包括肥皂:标题元素,而你的元素没有。这不太可能是这里的问题。

Also note that your working sample request includes a soap:Header element, and yours does not. That is unlikely to be the problem here though.

这篇关于使用Java Web Service的Objective C SOAP请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆