iphone - 요청 - request-promise-native
iPhone에서 JSON POST 요청(HTTPS 사용) (3)
Objective C를 전혀 모르지만, application/jsonrequest
대신에 Accept application/json
header를 application/json
application/jsonrequest
.
호스팅 된 WCF 서비스가 있고 JSON POST 요청으로 iPhone 응용 프로그램 내에서 사용하려고합니다. 나중에 JSON 시리얼 라이저를 사용할 계획이지만 이것이 요청에 대한 것입니다.
NSString *jsonRequest = @"{\"username\":\"user\",\"password\":\"letmein\"}";
NSLog(@"Request: %@", jsonRequest);
NSURL *url = [NSURL URLWithString:@"https://mydomain.com/Method/"];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];
NSData *requestData = [NSData dataWithBytes:[jsonRequest UTF8String] length:[jsonRequest length]];
[request setHTTPMethod:@"POST"];
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setValue:[NSString stringWithFormat:@"%d", [requestData length]] forHTTPHeaderField:@"Content-Length"];
[request setHTTPBody: requestData];
[NSURLConnection connectionWithRequest:[request autorelease] delegate:self];
내 데이터를받은 그냥 콘솔에 인쇄 해요 :
- (void) connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
NSMutableData *d = [[NSMutableData data] retain];
[d appendData:data];
NSString *a = [[NSString alloc] initWithData:d encoding:NSASCIIStringEncoding];
NSLog(@"Data: %@", a);
}
그리고이 응답 내에서 나는이 메시지를 얻습니다.
Data: <?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Request Error</title>
<style>
<!-- I've took this out as there's lots of it and it's not relevant! -->
</style>
</head>
<body>
<div id="content">
<p class="heading1">Request Error</p>
<p xmlns="">The server encountered an error processing the request. Please see the <a rel="help-page" href="https://mydomain.com/Method/help">service help page</a> for constructing valid requests to the service.</p>
</div>
</body>
</html>
왜 이런 일이 일어나고 어디서 잘못 될지 아는 사람이 있습니까?
대신에 ASIHTTPPost 를 사용하는 ASIHTTPPost 대해 읽었지만 iOS4에서 실행되도록 데모를 얻을 수 없습니다.
감사
편집하다:
저는 CharlesProxy 를 사용하여 iPhone (Simulator)에서 전화를 걸 때 일어나는 일에 대해 알아 보았습니다. 이것이 제가 준 것입니다 :
이상한 점은 SSL 프록시가이 호스트에 대해 활성화되지 않았다는 것입니다 : 프록시 설정, SSL 위치에서 사용 가능 "입니다. 아무도 이것이 무엇을 의미하는지 압니까? (이것은 또한 내 작업 Windows 클라이언트에서 발생합니다).
방금 Windows 클라이언트에서이 도구를 실행했는데 다른 점은 암호화 된 요청 및 응답 텍스트입니다. 보안 HTTP 연결을 사용하기 위해 무언가를 놓치고 있습니까?
새로운 수정 : http://maps.googleapis.com/maps/api/geocode/json?address=UK&sensor=true 와 같은 비 HTTPS 요청과 함께 작동합니다. 그래서 문제는 iPhone이 SSL을 통해 POST를 제대로 보내도록하는 것입니까?
나는 또한 이러한 방법을 사용하고 있습니다 :
- (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 canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace
{
if([[protectionSpace authenticationMethod] isEqualToString:NSURLAuthenticationMethodServerTrust])
{
return YES;
}
}
이것은 내가 이것을 멀리 할 수있게 해준다. 내가 사용하지 않으면 다음과 같은 오류 메시지가 나타납니다.
이 서버의 인증서가 유효하지 않습니다. 기밀 정보를 위험에 빠뜨릴 수있는 "mydomain.com"인 척하는 서버에 연결할 수 있습니다.
dic은 객체와 키가있는 NSMutable Dictionary입니다. baseUrl은 귀하의 서버 웹 서비스 URL입니다.
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dic
options:NSJSONWritingPrettyPrinted error:nil];
NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
NSString *encodedString = [jsonString base64EncodedString];
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@wsName",baseUrl]];
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];
[theRequest addValue: @"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
[theRequest setHTTPMethod:@"POST"];
[theRequest setValue:encodedString forHTTPHeaderField:@"Data"];
변화
NSData *requestData = [NSData dataWithBytes:[jsonRequest UTF8String] length:[jsonRequest length]];
에
NSData *requestData = [jsonRequest dataUsingEncoding:NSUTF8StringEncoding];