如何向ios中的soap webservice发送请求?

如何向ios中的soap webservice发送请求?,第1张

概述嗨,我想发送请求到soap webservice.我试图在AFnetworking并获得内部服务器错误500,现在我尝试在nsurlconnection但它也显示状态代码400.请任何人帮助我.谢谢提前听到的是我的代码. NSString *soapString = [NSString stringWithFormat: @" 嗨,我想发送请求到soap webservice.我试图在AFnetworking并获得内部服务器错误500,现在我尝试在nsurlconnection但它也显示状态代码400.请任何人帮助我.谢谢提前听到的是我的代码.

Nsstring *soapString = [Nsstring stringWithFormat:                                    @"<?xml version=\"1.0\" enCoding=\"utf-8\"?><soap12:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap12=\"http://www.w3.org/2003/05/soap-envelope\"><soap12:Body><addBusOrder xmlns=\"http://tempuri.org/\"><UserUniqueID>ok//8FjcvEKWMeeJJZHKYA</UserUniqueID><PlatFormID>4</PlatFormID><deviceid>E3D2FCF6-F41B-4275-BD34-FAA31307EFFE</deviceid><RouteScheduleID>532875503</RouteScheduleID><journeyDate>2016-03-25</journeyDate><FromCityID>734</FromCityID><ToCityID>202</ToCityID><TyPickUpID>22860424</TyPickUpID><Contactinfo><name>vinod</name><Email>[email protected]</Email><Phoneno>7842768497</Phoneno><mobile>8801720427</mobile></Contactinfo><pass><passenger><passengername>kumar</passengername><Age>23</Age><fare>1072</fare><Gender>M</Gender><Seatno>D3</Seatno></passenger></pass></addBusOrder></soap:Body></soap:Envelope></addBusOrder></soap12:Body></soap12:Envelope>"];            NSLog(@"soapString %@",soapString);    Nsstring *msgLength = [Nsstring stringWithFormat:@"%li",[soapString length]];    Nsstring *queryString = [Nsstring stringWithFormat: @"https://asprel.in/Service/AppServices.asmx/addBusOrder"];    NSURL *url = [NSURL URLWithString:queryString];    NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:url];    [req addValue:@"application/soap+xml; charset=utf-8" forhttpheaderFIEld:@"Content-Type"];    [req addValue:msgLength forhttpheaderFIEld:@"Content-Length"];    [req addValue:@"http://tempuri.org/addBusOrder" forhttpheaderFIEld:@"SOAPAction"];    [req sethttpMethod:@"POST"];    [req sethttpBody: [soapString dataUsingEnCoding:NSUTF8StringEnCoding]];    NSOperationQueue *queue = [[NSOperationQueue alloc]init];    [NSURLConnection sendAsynchronousRequest:req queue:queue completionHandler:^(NSURLResponse *response,NSData *data,NSError *connectionError) {        if (connectionError) {            NSLog(@"error %@",connectionError);        }else{            NSLog(@"data %@",[[Nsstring alloc]initWithData:data enCoding:NSUTF8StringEnCoding]);        }        NSXMLParser *parser = [[NSXMLParser alloc]initWithData:data];        [parser parse];    }];}

错误:

status code: 500,headers {    "Access-Control-Allow-Origin" = "*";    "Cache-Control" = private;    "Content-Length" = 322;    "Content-Type" = "text/plain; charset=utf-8";    Date = "Thu,24 Mar 2016 07:08:32 GMT";    Etag = "\"\"";    Server = "Microsoft-IIS/8.0";    "X-AspNet-Version" = "4.0.30319";
解决方法 您需要API授权

尝试添加授权

// Create the request NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData timeoutInterval:10];// New Create the connectionNSURLSessionConfiguration *defaultConfigObject = [NSURLSessionConfiguration defaultSessionConfiguration];NSURLSession *session = [NSURLSession sharedSession];//sessionWithConfiguration:defaultConfigObject delegate:self delegateQueue:[NSOperationQueue mainQueue]];//  NSURLCredential *creds = [NSURLCredential credentialWithUser:self.username password:self.password persistence:NSURLCredentialPersistenceForSession];Nsstring *authStr = [Nsstring stringWithFormat:@"%@:%@",username API,password API];// @"username:password";NSData *authData = [authStr dataUsingEnCoding:NSUTF8StringEnCoding];Nsstring *authValue = [Nsstring stringWithFormat: @"Basic %@",[authData base64EncodedStringWithOptions:0]];// Part important[request setValue:authValue forhttpheaderFIEld:@"Authorization"];[request addValue:@"application/soap+xml; charset=utf-8" forhttpheaderFIEld:@"Content-Type"];[request addValue:msgLength forhttpheaderFIEld:@"Content-Length"];[request addValue:@"http://tempuri.org/addBusOrder" forhttpheaderFIEld:@"SOAPAction"];[request sethttpMethod:@"POST"];[request sethttpBody: [soapString dataUsingEnCoding:NSUTF8StringEnCoding]];NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request         completionHandler:^(NSData *data,NSURLResponse *response,NSError *error) {                 NSXMLParser *parser = [[NSXMLParser alloc]initWithData:data];                [parser parse];                NSLog(@"%@",responseData);                    if (error) {                         [self handleError: error];                    } }];[dataTask resume];NSLog(@"header Request--->> %@",request.allhttpheaderFIElds);
总结

以上是内存溢出为你收集整理的如何向ios中的soap webservice发送请求?全部内容,希望文章能够帮你解决如何向ios中的soap webservice发送请求?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1029518.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-23
下一篇 2022-05-23

发表评论

登录后才能评论

评论列表(0条)

保存