2

追加の質問: 受信したデータの合計の長さは89973になりました。ただし、以下のコードを使用して文字列に変換すると、(null)文字列が返されます。解決済み:エンコーディングをNSASCIIStringEncodingに変更し、機能するようになりました。

NSString *responseString = [[NSString alloc] initWithData:receivedData encoding:NSUTF8StringEncoding];
NSLog(@"Received string: %@", responseString);

解決した質問: Webサイトにログインして、返されたデータを取得しようとしています。すべてが正常に機能しており、リダイレクトは正常に機能します。

また、didReceiveDataは複数回呼び出されていますが、奇妙なことに、データの長さがゼロです。didReceiveData内からデータ長をNSLogしても。

以下は、.mファイルと.hファイルの両方、および結果のNSLog情報です。

私はObjective-Cにかなり慣れておらず、これを何時間も修正しようとしていますが、原因を見つけることができないようです。

誰かが私を探してくれることを願っています。

これは.mファイルです

#import "SiteConnection.h"

@implementation SiteConnection

@synthesize username;
@synthesize password;

// Other
- (void) printInstanceVars {

}

- (void) getInformation {
    // Enable Network Indicator
    [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];

    // Set the request URL
    url = [NSURL URLWithString:@"http://url.com"];

    // Create the request using the URL defined above
    NSMutableURLRequest * request = [NSMutableURLRequest requestWithURL:url];

    // Change User-Agent to a Browser
    [request setValue:@"Mozilla/5.0 (Macintosh; Intel Mac OS X 10.7; rv:13.0) Gecko/20100101 Firefox/13.0.1" forHTTPHeaderField:@"User-Agent"];

    // Change method to POST
    [request setHTTPMethod:@"POST"];

    // Create POST string
    NSString *requestData = [NSString stringWithFormat:@"data=value"];

    // Append POST string to the request
    [request setHTTPBody: [requestData dataUsingEncoding:NSUTF8StringEncoding]];

    // Initialize the connection
    NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:NO];

    // Start the connection
    [connection start];
}

- (NSURLRequest *)connection:(NSURLConnection *)connection
         willSendRequest:(NSURLRequest *)request
        redirectResponse:(NSURLResponse *)response
{
    NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
    int statusCode = [httpResponse statusCode];

    // http statuscodes between 300 & 400 is a redirect ...
    if (response && statusCode >= 300 && statusCode < 400) {
        NSLog(@"Redirecting to : %@ (%u)", [request URL], statusCode);
    }

    return request;
}

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
    [receivedData setLength:0];

    NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
    NSLog(@"Response: %u (%@)", [httpResponse statusCode], [NSHTTPURLResponse localizedStringForStatusCode:[httpResponse statusCode]]);
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
    [receivedData appendData:data];
    NSLog(@"Receiving data... Length: %d", [receivedData length]);
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
    NSLog(@"Error: %@", [error localizedDescription]);
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
    // Hide Network Indicator
    [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];

    // Display data length
    NSLog(@"Total received data: %d", [receivedData length]);

    // Convert data into string and display it
    NSString *responseString = [[NSString alloc] initWithData:receivedData encoding:NSUTF8StringEncoding];
    NSLog(@"Received string: %@", responseString);
    //NSLog(@"Cookies: %@", [[NSHTTPCookieStorage sharedHTTPCookieStorage] description]);
}

これは.hファイルです

#import <Foundation/Foundation.h>

@class StatusViewController;

@interface SiteConnection : NSObject {
    NSString *username;
    NSString *password;
    NSURL *url;
    NSMutableData *receivedData;
}

@property NSString *username;
@property NSString *password;

// Other
- (void) printInstanceVars;
- (void) getInformation;

@end

結果のNSLogは次のとおりです。

2012-07-02 15:41:12.578 Vodafone[25051:11303] Response: 200 (no error)
2012-07-02 15:41:12.578 Vodafone[25051:11303] Receiving data... Length: 0
2012-07-02 15:41:12.580 Vodafone[25051:11303] Receiving data... Length: 0
2012-07-02 15:41:12.580 Vodafone[25051:11303] Receiving data... Length: 0
2012-07-02 15:41:12.582 Vodafone[25051:11303] Receiving data... Length: 0
2012-07-02 15:41:12.583 Vodafone[25051:11303] Receiving data... Length: 0
2012-07-02 15:41:12.585 Vodafone[25051:11303] Receiving data... Length: 0
2012-07-02 15:41:12.586 Vodafone[25051:11303] Receiving data... Length: 0
2012-07-02 15:41:12.587 Vodafone[25051:11303] Receiving data... Length: 0
2012-07-02 15:41:12.590 Vodafone[25051:11303] Receiving data... Length: 0
2012-07-02 15:41:12.590 Vodafone[25051:11303] Receiving data... Length: 0
2012-07-02 15:41:12.591 Vodafone[25051:11303] Receiving data... Length: 0
2012-07-02 15:41:12.592 Vodafone[25051:11303] Receiving data... Length: 0
2012-07-02 15:41:12.596 Vodafone[25051:11303] Receiving data... Length: 0
2012-07-02 15:41:12.601 Vodafone[25051:11303] Receiving data... Length: 0
2012-07-02 15:41:12.605 Vodafone[25051:11303] Receiving data... Length: 0
2012-07-02 15:41:12.608 Vodafone[25051:11303] Receiving data... Length: 0
2012-07-02 15:41:12.608 Vodafone[25051:11303] Total received data: 0
2012-07-02 15:41:12.608 Vodafone[25051:11303] Received string: 
4

3 に答える 3

0

あなたが忘れているようです:

NSMutableData *receivedData = [[NSMutableData alloc] init];
于 2012-07-02T13:51:13.350 に答える
0

あなたが決して初期化しないように私には見えますreceivedData。したがって、受信したデータを明らかに効果のないnilポインタに追加します;-)receivedData = [[NSMutableData alloc] init];使用する前に次のようなことを試してください。

于 2012-07-02T13:52:18.680 に答える
0

これを行う:

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
    if (!receivedData)
    {
        receivedData = [NSMutableData data];
    }

    [receivedData appendData:data];

    NSLog(@"Receiving data... Length: %d", [receivedData length]);
}
于 2012-07-02T13:57:45.260 に答える