Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #import "HttpsPost.h"
- #import "APIURL.h"
- @implementation HttpsPost
- @synthesize webData=_webData;
- @synthesize requestData=_requestData;
- -(void)getHttpsPostDataWithMessageString:(NSString *)soapMessage methodName:(NSString *)methodName {
- NSLog(@"soapMessage%@",soapMessage);
- //请求发送到的路径
- NSURL *httpsurl = [NSURL URLWithString:[[NSString alloc]initWithFormat:@"%@/%@",WEB_SERVICE_URL,methodName]];
- NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:httpsurl];
- [theRequest setHTTPMethod:@"POST"];
- [theRequest setHTTPBody: [soapMessage dataUsingEncoding:NSUTF8StringEncoding]];
- //请求
- NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self startImmediately:NO];
- NSPort* port = [NSPort port];
- NSRunLoop* rl = [NSRunLoop currentRunLoop]; // Get the runloop
- [rl addPort:port forMode:NSDefaultRunLoopMode];
- [theConnection scheduleInRunLoop:rl forMode:NSRunLoopCommonModes];
- [theConnection start];
- if (theConnection) {
- self.webData = [NSMutableData data] ;
- }else{
- NSLog(@"theConnection is NULL");
- }
- while(!finish) {
- [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
- }
- }
- -(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
- {
- NSLog(@"123");
- NSHTTPURLResponse *HTTPresponse = (NSHTTPURLResponse *)response;
- NSInteger statusCode = [HTTPresponse statusCode];
- if ( 404 == statusCode || 500 == statusCode ) {
- finish=YES;
- [connection cancel];
- } else {
- [self.webData setLength: 0];
- }
- }
- -(BOOL)connection:(NSURLConnection *)connection canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace
- {
- return [protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust];
- }
- -(void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
- {
- if ([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust])
- [challenge.sender useCredential:[NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust]
- forAuthenticationChallenge:challenge];
- [challenge.sender continueWithoutCredentialForAuthenticationChallenge:challenge];
- }
- -(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
- {
- [self.webData appendData:data];
- NSLog(@"connection: didReceiveData:2");
- }
- //如果电脑没有连接网络,则出现此信息(不是网络服务器不通)
- -(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
- {
- NSLog(@"error didFailWithError :%@",error);
- finish=YES;
- NSLog(@"ERROR with theConenction");
- }
- -(void)connectionDidFinishLoading:(NSURLConnection *)connection
- {
- NSLog(@"3 DONE. Received Bytes: %d", [self.webData length]);
- self.requestData = self.webData;
- finish=YES;
- NSString *log= [[NSString alloc] initWithBytes: [self.webData mutableBytes] length:[self.webData length] encoding:NSUTF8StringEncoding];
- NSLog(@"https requestback:%@",log);
- }
- -(NSData *)getRequestData{
- return self.requestData;
- }
- @end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement