如何使用SOAP Web服务向服务器发送HTTPS同步请求

时间:2012-03-20 13:35:11

标签: iphone objective-c ios

代码:

NSString *urlString=@"http://projdemo.com/MobileWebServices/Service.asmx";  
soapRequestStr = [self CreateRequest];  
NSString* soapAction;  
soapAction =@"http://www.projdemo.com/FetchAll";  
urlRequest=NULL;   
responseData=NULL;  
httpBodyData=[soapRequestStr dataUsingEncoding: NSUTF8StringEncoding];    
url=[[NSURL alloc]initWithString: urlString];     
urlRequest = [NSMutableURLRequest requestWithURL:url];    
[urlRequest setHTTPMethod:@"POST"];    
[urlRequest setHTTPBody:httpBodyData];  
[urlRequest setValue:@"projdemo.com" forHTTPHeaderField:@"Host"];  
[urlRequest setValue:@"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];  
[urlRequest setValue:[NSString stringWithFormat:@"%d",[soapRequestStr length]]                
forHTTPHeaderField:@"Content-Length"];  
[urlRequest setValue:soapAction forHTTPHeaderField:@"SOAPAction"];  
responseData = [NSURLConnection sendSynchronousRequest:urlRequest returningResponse:nil    
error:nil];  
response = nil;  
error = nil;      
responseString=NULL;  
responseString = [[NSString alloc] initWithData:responseData   
encoding:NSASCIIStringEncoding];  
NSLog(@"data is %@",responseData);  
NSLog(@"Response string is %@",responseString);      

soapRequestStr是我们要传递给服务器的XML文件格式化字符串。这里的工作正常,当URL更改为https时获取响应为

错误回复:

<html><head>  
<title>400 Bad Request</title>  
</head><body>  
<h1>Bad Request</h1>  
<p>Your browser sent a request that this server could not understand.<br />  
</p>  
<hr>  
<address>Apache/2.2.20 (Ubuntu) Server at projdemo.com Port 443</address>  
</body></html>  

1 个答案:

答案 0 :(得分:0)

Parsing xml:

NSXMLParser *parser;

parser = [[NSXMLParser alloc]initWithContentsOfURL:

[NSURLURLWithString:@"urPath/webservice"]];

[parser setDelegate:self];

[parser parse];

// THEN USE THESE DELEGATES

-(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
    {
            NSLog("%@",elementName);//starting tag
    }


-(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
    {
        NSLog("%@",string);//THE VALUE
    }

-(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
    {
        NSLog("%@",elementName);//END TAG
    }