首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用SOAP SOAP服务

使用SOAP SOAP服务
EN

Stack Overflow用户
提问于 2015-08-20 22:27:54
回答 1查看 36关注 0票数 0

您好,我正在尝试实现一个SOAP调用,使用一个示例:

代码语言:javascript
复制
- (void)viewDidLoad {
    [super viewDidLoad];

    NSString  *textFieldCelcisus = @"0";
    NSString *soapMessage = [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"
                             " <CelsiusToFahrenheit xmlns=\"http://tempuri.org/\">\n"
                             "<Celsius>%@</Celsius>\n"
                             "</CelsiusToFahrenheit>\n"
                             "</soap:Body>\n"
                             "</soap:Envelope>\n" ,textFieldCelcisus];

    NSURL *url = [NSURL URLWithString:@"http://w3schools.com/webservices/tempconvert.asmx"];
    NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];
    NSString *msgLength = [NSString stringWithFormat:@"%lu", (unsigned long)[soapMessage length]];

    [theRequest addValue: @"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
    [theRequest addValue: @"http://www.w3schools.com/webservices/CelsiusToFahrenheit" 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 )
    {
        NSLog(@"Ok");
    }
    else
    {
        NSLog(@"theConnection is NULL");
    }
    // Do any additional setup after loading the view, typically from a nib.
}

在控制台上是说"Ok",但是如果我放了一个错误的url,它仍然是"Ok“,我不明白为什么。

编辑

更新,我做了委托方法,我看不到错误,但消息仍然是"Ok“

代码语言:javascript
复制
#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController


- (void)viewDidLoad {


    receivedData = [NSMutableData dataWithCapacity: 0];

    [super viewDidLoad];


    NSString  *textFieldCelcisus = @"0";
    NSString *soapMessage = [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"
                             " <CelsiusToFahrenheit xmlns=\"http://tempuri.org/\">\n"
                             "<Celsius>%@</Celsius>\n"
                             "</CelsiusToFahrenheit>\n"
                             "</soap:Body>\n"
                             "</soap:Envelope>\n" ,textFieldCelcisus];

    NSURL *url = [NSURL URLWithString:@"http://w3schools.com/webservices/tempconvert.asmx"];
    NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];
    NSString *msgLength = [NSString stringWithFormat:@"%lu", (unsigned long)[soapMessage length]];

    [theRequest addValue: @"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
    [theRequest addValue: @"http://www.w3schools.com/webservices/CelsiusToFahrenheit" forHTTPHeaderField:@"SOAPAction"];
    [theRequest addValue: msgLength forHTTPHeaderField:@"Content-Length"];
    [theRequest setHTTPMethod:@"POST"];
    [theRequest setHTTPBody: [soapMessage dataUsingEncoding:NSUTF8StringEncoding]];

    theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
    if( theConnection ) //pointer returned from the [[NSURLConnection  alloc] init]
    {
        NSLog(@"Ok");//as long as the initializer returns not nil, this will print.
    }
    else
    {
        NSLog(@"theConnection is NULL");
    }
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    // This method is called when the server has determined that it
    // has enough information to create the NSURLResponse object.

    // It can be called multiple times, for example in the case of a
    // redirect, so each time we reset the data.

    // receivedData is an instance variable declared elsewhere.
    [receivedData setLength:0];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    // Append the new data to receivedData.
    // receivedData is an instance variable declared elsewhere.
    [receivedData appendData:data];
}

- (void)connection:(NSURLConnection *)connection
  didFailWithError:(NSError *)error
{
    // Release the connection and the data object
    // by setting the properties (declared elsewhere)
    // to nil.  Note that a real-world app usually
    // requires the delegate to manage more than one
    // connection at a time, so these lines would
    // typically be replaced by code to iterate through
    // whatever data structures you are using.
    theConnection = nil;
    receivedData = nil;

    // inform the user
    NSLog(@"Connection failed! Error - %@ %@",
          [error localizedDescription],
          [[error userInfo] objectForKey:NSURLErrorFailingURLStringErrorKey]);
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    // do something with the data
    // receivedData is declared as a property elsewhere
    NSLog(@"Succeeded! Received %lu bytes of data",(unsigned long)[receivedData length]);

    // Release the connection and the data object
    // by setting the properties (declared elsewhere)
    // to nil.  Note that a real-world app usually
    // requires the delegate to manage more than one
    // connection at a time, so these lines would
    // typically be replaced by code to iterate through
    // whatever data structures you are using.
    theConnection = nil;
    receivedData = nil;
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
EN

回答 1

Stack Overflow用户

发布于 2015-08-20 23:11:14

您的逻辑是查看"theConnection“是否是有效的指针。

代码语言:javascript
复制
NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
    if( theConnection ) //pointer returned from the [[NSURLConnection  alloc] init]
    {
        NSLog(@"Ok");//as long as the initializer returns not nil, this will print.
    }
    else
    {
        NSLog(@"theConnection is NULL");
    }

您将需要为NSURLConnection实现委托方法,以查看请求的结果。https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/URLLoadingSystem/Tasks/UsingNSURLConnection.html

连接:didFailWithError:

连接:didReceiveData:

connectionDidFinishLoading:

等等。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32120973

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档