2013-11-21 5 views
0

문제가 있습니다. 나는 프로그램에서 (xcode, 목표 C로 개발 됨) 요청을 보내고 데이터가 전송되었지만 사이트 (인증)에 등록해야하고 잘 모르겠습니다.웹 사이트에 데이터 전송

전체에서
NSString * param = [NSString stringWithFormat:@"action=login&name=%@&password=%@", myName, myPassword] 

내 코드 :

request.HTTPMethod = @"POST"; 

NSString * myName = _loginLogin.text; 
NSString * myPassword= _passwordLogin.text; 

NSString * param = [NSString stringWithFormat:@"action=login&name=%@&password=%@", myName, myPassword]; 
request.HTTPBody = [param dataUsingEncoding:NSUTF8StringEncoding]; 

NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self]; 

나는이 * (www.dnevnik.ru)와 함께 작동 * 페이지를 참조하십시오.

그리고 내 사이트 문자열 NSString *postString = [NSString stringWithFormat:@"&username=%@&password=%@", username, password];에 적합하지 않겠습니까?

+0

당신은 NSURLConnectionDelegate의 대리인 콜백에서 응답을 구문 분석 할 필요가 당신의 .H 파일에서

@property (nonatomic, strong) NSMutableData *responseData; 

를 추가 – Tim

답변

1
코드 다음

사용 : 그냥

NSUserDefaults *loginData = [NSUserDefaults standardUserDefaults]; 
    NSString *username = [loginData objectForKey:@"username"] ; 
    NSString *password = [loginData objectForKey:@"password"]; 

    NSString *postString = [NSString stringWithFormat:@"&username=%@&password=%@", username, password]; 
    NSString *urlString = @"http://YourUrl"; 
    NSURL *myURL = [NSURL URLWithString:[urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]; 

    NSMutableURLRequest *detailRequestToServer =[NSMutableURLRequest requestWithURL:myURL cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:60.0]; 
    [detailRequestToServer setHTTPMethod:@"POST"]; 
    [detailRequestToServer setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"]; 
    const char *utfString = [postString UTF8String]; 
    NSString *utfStringLenString = [NSString stringWithFormat:@"%zu", strlen(utfString)]; 
    [detailRequestToServer setHTTPBody:[NSData dataWithBytes: utfString length:strlen(utfString)]]; 
    [detailRequestToServer setValue:utfStringLenString forHTTPHeaderField:@"Content-Length"]; 

    NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:detailRequestToServer delegate:self]; 
    if (theConnection) 
    { 
     self.responseData = [[NSMutableData alloc] init]; 
    } 
    else 
     NSLog(@"Connection Failed!"); 
0
NSString *bodyData = [NSString stringWithFormat:@"action=login&name=%@&password=%@", myName, myPassword]; 

    NSMutableURLRequest *postRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"https://www.apple.com"]]; //replace your url here 

    // Set the request's content type to application/x-www-form-urlencoded 
    [postRequest setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"]; 

    // Designate the request a POST request and specify its body data 
    [postRequest setHTTPMethod:@"POST"]; 
    [postRequest setHTTPBody:[NSData dataWithBytes:[bodyData UTF8String] length:strlen([bodyData UTF8String])]]; 

// Create the NSMutableData to hold the received data. 
// receivedData is an instance variable declared elsewhere. 
receivedData = [NSMutableData dataWithCapacity: 0]; 

// create the connection with the request 
// and start loading the data 
NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:postRequest delegate:self]; 
if (!theConnection) { 
    // Release the receivedData object. 
    receivedData = nil; 

    // Inform the user that the connection failed. 
} 
관련 문제