Http Post Request

前端 未结 2 1465
夕颜
夕颜 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: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.
    }
    

提交回复
热议问题