Http Post Request

前端 未结 2 1460
夕颜
夕颜 2020-12-20 07:30

How to make Http Post Request using JSON. I tried every option which is available on Internet.But could not get the Data. So please post entire code to make a request.

相关标签:
2条回答
  • 2020-12-20 08:17

    You can use below code:

    
    -(void)performRequest{
        NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
        NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"url"]];
        NSString *msgLength = [NSString stringWithFormat:@"%d", [jsonMessage length]];
        [request addValue: @"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
        [request addValue: jsonAction forHTTPHeaderField:@"JSONAction"];
        [request addValue: msgLength forHTTPHeaderField:@"Content-Length"];
        [request setHTTPMethod:@"POST"];
        [request setHTTPBody: [jsonMessage dataUsingEncoding:NSUTF8StringEncoding]];
        NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
        if( theConnection )
        {
            webData = [[NSMutableData data] retain];
        }
        else
        {
            NSLog(@"theConnection is NULL");
        }
        [pool release];
    }
    
    -(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{
        [webData setLength: 0];
        self.resultArray = [[NSMutableArray alloc] init];
    }
    -(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
        [webData appendData:data];
    }
    -(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{
        NSLog(@"ERROR with theConenction");
        NSDictionary *errorDic = [NSDictionary dictionaryWithObject:error forKey:@"error"];
        [self.resultArray addObject:errorDic];
        [connection release];
        [webData setLength:0];
    }
    -(void)connectionDidFinishLoading:(NSURLConnection *)connection{
        NSLog(@"DONE. Received Bytes: %d", [webData length]);
        NSString *theXML = [[NSString alloc] initWithBytes: [webData mutableBytes] length:[webData length] encoding:NSUTF8StringEncoding];
        NSLog(@"%@", theXML);
        [theXML release];
        if([webData length] > 0){
            parser = [[NSXMLParser alloc] initWithData:webData];
            [parser setDelegate:self];
            [parser parse]; 
        }
    }
    
    0 讨论(0)
  • 2020-12-20 08:28

    Here's a basic example of NSURLConnection POST-ing JSON to an URL.

    - (void)performRequest {
        NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"http://someplace.com/"]];
        [request setValue:@"Some Value" forHTTPHeaderField:@"Some-Header"];
        [request setHTTPBody:@"{\"add_json\":\"here\"}"];
        [request setHTTPMethod:@"POST"];
        [NSURLConnection connectionWithRequest:[request autorelease] delegate:self];
    }
    
    - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
         // Fail..
    }
    
    - (void)connectionDidFinishLoading:(NSURLConnection *)connection {
         // Request performed.
    }
    
    0 讨论(0)
提交回复
热议问题