iOS 使用ASIHTTPRequest 发送Webservice请求

直接上代码

#import <ASIHTTPRequest.h>
#import <ASIFormDataRequest.h>
@interface ViewController ()<ASIHTTPRequestDelegate>
@property (nonatomic, strong) ASIHTTPRequest *request;
@end
- (void)xsNetworkReq
{
    NSString *mess = [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"
                      "<selectIsUserOk xmlns=\"http://tempuri.org/\">\n"
                      "<U_no>%@</U_no>\n"
                      "<U_pass>%@</U_pass>\n"
                      "</selectIsUserOk>\n"
                      "</soap:Body>\n"
                      "</soap:Envelope>\n", @"", @""
                      ];
// NSLog(@"%@", mess);
    //请求发送到的路径
    NSURL *url = [NSURL URLWithString:@""];
    NSString *soapActionURL = @"";
    self.request = [[ASIHTTPRequest alloc] initWithURL:url];
    NSString *msgLength = [NSString stringWithFormat:@"%lu", (unsigned long)[mess length]];
    // 以下对请求信息添加属性前四句是必有的,第五句是soap信息。
    [self.request addRequestHeader:@"Host" value:[url host]];
    [self.request addRequestHeader:@"Content-Length" value:msgLength];
    [self.request addRequestHeader:@"Content-Type" value:@"text/xml; charset=utf-8"];
    [self.request addRequestHeader:@"SOAPAction" value:[NSString stringWithFormat:@"%@",soapActionURL]];
    [self.request setRequestMethod:@"POST"];
    //传soap信息
    [self.request appendPostData:[mess dataUsingEncoding:NSUTF8StringEncoding]];
    [self.request setValidatesSecureCertificate:NO];
    [self.request setTimeOutSeconds:60.0];
    [self.request setDefaultResponseEncoding:NSUTF8StringEncoding];
    self.request.delegate = self;

    [self.request startAsynchronous];
}
- (void)requestStarted:(ASIHTTPRequest *)request { NSLog(@"开始"); } - (void)requestFinished:(ASIHTTPRequest *)request { NSString *str = [request responseString]; NSLog(@"result-------%@", str); } - (void) FaileddidRequestError:(NSError *)error { NSLog(@"失败"); } 

你可能感兴趣的:(Web,service)