【问题标题】:Send Data to Web Site发送数据到网站
【发布时间】:2013-11-21 12:21:31
【问题描述】:

我有问题。我从程序(xcode,我在目标C上开发)向服务器发送POST请求,数据已发送,但我需要在网站上注册(身份验证),我不知道,我是如何发送这个字符串的

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]; 不合适?

【问题讨论】:

  • 您需要在 NSURLConnectionDelegate 的委托回调中解析响应

标签: html ios iphone objective-c


【解决方案1】:

使用以下代码: 只需添加

@property (nonatomic, strong) NSMutableData *responseData;

在您的 .h 文件中

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!");

【讨论】:

    【解决方案2】:
      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.
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-25
      • 2019-08-07
      • 2013-07-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-01
      • 1970-01-01
      相关资源
      最近更新 更多