HTTP
요청 post
및 get
응답을 간단한 목표 C 코드 방법을 사용하여 생성하려고합니다. 아래 코드는 내가 성공적으로 게시 할 수 있습니다. 하지만 응답 데이터를받지 못했습니다. 응답 인쇄 null
값만. 제발 도와주세요, 응답 데이터를 받아야합니다. 내 코드를 매우 간단NULL 값을 표시하는 HTTP 요청 GET 응답
NSString *post = [NSString stringWithFormat:@"name=%@",name];
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%d",[postData length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc]init];
[request setURL:[NSURL URLWithString:[NSString stringWithFormat:URLPATH]]];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setHTTPBody:postData];
NSURLConnection *conn = [[NSURLConnection alloc]initWithRequest:request delegate:self];
if(conn) {
NSLog(@"Connection Successful");
} else {
NSLog(@"Connection could not be made");
}
// Create GET method
NSError *err;
NSURLResponse *responser;
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&responser error:&err];
// JSON Formatter
NSError *error;
NSDictionary *jsonsDictionary = [NSJSONSerialization JSONObjectWithData:responseData options:kNilOptions error:&error];
NSDictionary *respon = [jsonsDictionary objectForKey:@"response"];
NSLog(@"UPDATE RESPONSE : %@", respon);
응답이 유효 JSON되지 않습니다 : 그래서이 경우 당신은 데이터를 문자열로 변환하고 컨텐츠 유형 헤더를 설정
"application/x-www-form-urlencoded"
또는 사용 다음 코드로 요청 헤더"Content-Type"
을 설정할 필요가 없다. responseData에서 NSString을 생성하고이를 출력하십시오. 또한 오류 및 오류 변수를 인쇄하십시오. –일부 코드는 게시 할 수 있습니까? 나는 너를 못 잡는다. – Mano
NSLog (@ "% @", err); 및 NSLog (@ "% @", 오류); –