当前位置: 首页 > 面试题库 >

Java Web Service的目标C SOAP请求

许高峻
2023-03-14
问题内容

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

@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)

方法返回

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中完成了此请求:

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请求无效。你有

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

但是您应该拥有(请注意,删除无效的XML标签终止):

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

还要注意,您的工作示例请求中包含soap:Header元素,而您没有。不过,这不太可能成为这里的问题。



 类似资料:
  • 我已经安装了最新的cordova。版本是4.3.0。我创建了一个空的Cordova项目: 然后我添加平台: 我用以下方法构建了该项目: 但我总是得到: 然后,我尝试将目标android sdk更改为19,因此在AndroidManifest.xml中,我更改了: 自: 我再次运行build命令,仍然得到完全相同的错误。这快把我逼疯了。有人有什么想法吗?我在同一台PC上构建了许多许多cordova项

  • 本文向大家介绍C# 调用 JavaWebservice服务遇到的问题汇总,包括了C# 调用 JavaWebservice服务遇到的问题汇总的使用技巧和注意事项,需要的朋友参考一下 1. A SOAP 1.2 message is not valid when sent to a SOAP 1.1 only endpoint. 问题原因: 客户端和服务端的SOAP协议版本不一致。 解决方法: ①修改

  • 我正在使用restTemplate发出post请求,并收到以下错误:无法找到到请求目标的有效证书路径 我的方法如下:

  • 我的应用程序是用1.5.6版本的Java-JAR启动的。spring Boot的发行版。 我的一个请求的内容带有字符“{”。当它发送到服务器时,会引发以下异常: IllegalArgumentException:在请求目标中发现无效字符。有效字符定义于RFC 7230和RFC 3986中,地址为org.apache.coyote.http11.http11inputbuffer.parserequ

  • 我在Apache Tomcat7上有一个web应用程序,我的web应用程序上有不可信的证书。我的web应用程序必须与另一个使用HTTPS的web应用程序通信。然而,我总是遇到这样一个例外: 连接失败:javax.net.ssl.sslhandShakeException:sun.security.validator.validatoreXception:PKIX路径构建失败:sun.securit

  • 我使用调用登录请求。在登录测试中,我从令牌登录响应中设置了环境变量。这不起作用。登录测试成功,但环境变量令牌不存在。 集合中的预请求脚本: 登录请求中的测试脚本: