passing parameter to the web service working code
我再次尝试使用SOAP方法,但这次我下载了Charles并观察了我从代码中创建的请求。chales显示"分析数据失败(org.xml.sax.saxparser异常:prolog中不允许有内容)"
这是我的代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 | NSString *soapMsg = [[NSString alloc] initWithFormat:@" " " " "" " " "%@ " "%@ " "%@ " "%@ " " " " " " ",str1,str2,str3,str4]; NSLog(soapMsg); NSURL *url = [NSURL URLWithString:@"http://192.168.0.218:84/WebServiceCustomerByAmit/Service.asmx?op=InsertCustomerInformation"]; NSLog(@"url. . . .%@", url); NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:url]; NSLog(@"req....%@", req); NSString *msgLength = [NSString stringWithFormat:@"%d", [soapMsg length]]; NSLog(@"msgLength. . .%@", msgLength); [req addValue: @"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"]; [req addValue: @"http://tempuri.org/InsertCustomerInformation" forHTTPHeaderField:@"SOAPAction"]; [req addValue: msgLength forHTTPHeaderField:@"Content-Length"]; [req setHTTPMethod:@"POST"]; [req setHTTPBody: [soapMsg dataUsingEncoding:NSUTF8StringEncoding]]; NSLog(@"req....%@", req); NSError *error; NSURLResponse *response; //NSData *urlData=[NSURLConnection sendSynchronousRequest:req returningResponse:&response error:&error]; NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:req delegate:self]; if( theConnection ) { //webData = [[NSMutableData data] retain]; NSLog(@"theConnection is OK"); } else { NSLog(@"theConnection is NULL"); } if(!response){ UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Connection Error" message:@"Failed to Connect to the Internet" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil]; [alert show]; [alert release]; } else{ UIAlertView *alert1 = [[UIAlertView alloc] initWithTitle:@"Connection Successful" message:@"Connected to the Internet" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil]; [alert1 show]; [alert1 release]; } } |
救命!!提前谢谢!!
你在问如何在你的项目中包含asihttpRequest,对吗?将asihttpRequest的文件夹复制并粘贴到项目的文件夹中,然后将其添加到代码中:
1 | #import"ASIHTTPRequest.h" |
请注意,asihttpRequest是一个外部库,不是iOS SDK的一部分。