Advertisement
Guest User

http post

a guest
Jul 12th, 2012
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #import "HttpsPost.h"
  2. #import "APIURL.h"
  3. @implementation HttpsPost
  4. @synthesize webData=_webData;
  5. @synthesize requestData=_requestData;
  6. -(void)getHttpsPostDataWithMessageString:(NSString *)soapMessage methodName:(NSString *)methodName  {
  7.  
  8.     NSLog(@"soapMessage%@",soapMessage);
  9.     //请求发送到的路径
  10.     NSURL *httpsurl = [NSURL URLWithString:[[NSString alloc]initWithFormat:@"%@/%@",WEB_SERVICE_URL,methodName]];
  11.     NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:httpsurl];
  12.     [theRequest setHTTPMethod:@"POST"];
  13.     [theRequest setHTTPBody: [soapMessage dataUsingEncoding:NSUTF8StringEncoding]];
  14.  
  15.     //请求
  16.     NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self startImmediately:NO];
  17.     NSPort* port = [NSPort port];  
  18.     NSRunLoop* rl = [NSRunLoop currentRunLoop]; // Get the runloop
  19.     [rl addPort:port forMode:NSDefaultRunLoopMode];
  20.     [theConnection scheduleInRunLoop:rl forMode:NSRunLoopCommonModes];
  21.     [theConnection start];
  22.    
  23.     if (theConnection) {
  24.         self.webData = [NSMutableData data] ;
  25.     }else{
  26.         NSLog(@"theConnection is NULL");
  27.     }
  28.     while(!finish) {
  29.         [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
  30.     }
  31.  
  32. }
  33.  
  34. -(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
  35. {
  36.     NSLog(@"123");
  37.     NSHTTPURLResponse *HTTPresponse = (NSHTTPURLResponse *)response;  
  38.     NSInteger statusCode = [HTTPresponse statusCode];  
  39.     if ( 404 == statusCode || 500 == statusCode ) {
  40.         finish=YES;
  41.         [connection cancel];
  42.     } else {
  43.         [self.webData setLength: 0];
  44.     }
  45. }
  46.  
  47.  
  48. -(BOOL)connection:(NSURLConnection *)connection canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace
  49. {
  50.     return [protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust];
  51. }
  52.  
  53. -(void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
  54. {
  55.     if ([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust])
  56.         [challenge.sender useCredential:[NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust]
  57.              forAuthenticationChallenge:challenge];
  58.    
  59.     [challenge.sender continueWithoutCredentialForAuthenticationChallenge:challenge];
  60. }
  61. -(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
  62. {
  63.     [self.webData appendData:data];
  64.     NSLog(@"connection: didReceiveData:2");
  65.    
  66. }
  67.  
  68. //如果电脑没有连接网络,则出现此信息(不是网络服务器不通)
  69. -(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
  70. {
  71.     NSLog(@"error didFailWithError :%@",error);
  72.     finish=YES;
  73.     NSLog(@"ERROR with theConenction");
  74.  
  75. }
  76. -(void)connectionDidFinishLoading:(NSURLConnection *)connection
  77. {
  78.     NSLog(@"3 DONE. Received Bytes: %d", [self.webData length]);
  79.     self.requestData = self.webData;
  80.     finish=YES;
  81.    
  82.     NSString *log= [[NSString alloc] initWithBytes: [self.webData mutableBytes] length:[self.webData length] encoding:NSUTF8StringEncoding];
  83.    
  84.    
  85.     NSLog(@"https requestback:%@",log);
  86.  
  87.    
  88. }
  89. -(NSData *)getRequestData{
  90.     return self.requestData;
  91. }
  92.  
  93. @end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement