そのため、フォーム自体を機能させることはできませんでしたが、機能するように作り直すことができました。いくつかのことに注意してください。
- ARC に変換する必要があります。
- 後で解放できるように、接続への強力な参照が必要です(デリゲートメソッドではありません!)
- デリゲート connectionSucceeded メソッドが必要です(応答を記録するために!)
コード:
- (void)asynchronousRequest
{
[activity startAnimating];
NSString *requesturl = lblURL.text;
NSLog(@"requesturl=%@", requesturl);
NSURL *theURL = [NSURL URLWithString:requesturl];
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setValue:@"application/x-www-form-urlencoded; charset=utf-8" forHTTPHeaderField:@"content-type"];
[request setURL:theURL];
[request setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];
[request setTimeoutInterval:60.0];
[request setHTTPMethod:@"POST"];
NSString *str = [NSString stringWithFormat:@"key=%@", [self URLencodedString:@"Abcd1234"]];
NSLog(@"BODY: %@", str);
NSData *body = [str dataUsingEncoding:NSUTF8StringEncoding];
NSLog(@"URL : %@", requesturl);
NSLog(@"REQ : %@", request);
[request setHTTPBody:body];
[request addValue:[NSString stringWithFormat:@"%u", [body length]] forHTTPHeaderField:@"Content-Length"];
NSLog(@"AllFields : %@", [request allHTTPHeaderFields]);
NSLog(@"HTTPBody : %@", [[NSString alloc] initWithData:[request HTTPBody] encoding:NSUTF8StringEncoding]);
NSLog(@"HTTPMethod : %@", [request HTTPMethod]);
self.activeDownload = [NSMutableData data];
conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];
assert(conn);
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
assert([response isKindOfClass:[NSHTTPURLResponse class]]);
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
NSLog(@"GOT %d", [httpResponse statusCode]);
}
- (NSString *)URLencodedString:(NSString *)s
{
CFStringRef str = CFURLCreateStringByAddingPercentEscapes(NULL, (CFStringRef)s, NULL, (CFStringRef)@"!*'();:@&;=+$,/?%#[]", kCFStringEncodingUTF8);
NSString *newString = [(NSString *)str stringByReplacingOccurrencesOfString:@" " withString:@"+"];
if(str) CFRelease(str);
return newString;
}
編集:まだ機能しなかった変更されたコード:
- (void)asynchronousRequest
{
[activity startAnimating];
NSString *boundary = @"1010101010"; // DFH no need for the leading '--'
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
NSMutableDictionary *postVariables = [[NSMutableDictionary alloc] init];
[postVariables setValue:@"Abcd1234" forKey:@"key"];
NSString *requesturl = lblURL.text;
NSMutableString *myStr = [[NSMutableString alloc] init];
NSString *str;
// DFH - strategy is to have each line append its own terminating newline/return
str = [NSString stringWithFormat:@"--%@\r\n",boundary]; // DFH initial boundary
[myStr appendString:str];
NSArray *formKeys = [postVariables allKeys];
for (int i = 0; i < [formKeys count]; i++) {
str = [NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"\r\n%@\r\n",[formKeys objectAtIndex:i],[postVariables valueForKey:[formKeys objectAtIndex:i]]];
[myStr appendString:str];
str = [NSString stringWithFormat:@"--%@\r\n",boundary]; // DFH mid or terminating boundary
[myStr appendString:str];
}
NSLog(@"BODY: %@", myStr);
NSData *body = [myStr dataUsingEncoding:NSUTF8StringEncoding];
requesturl = [self encodeStringForURL:requesturl];
NSLog(@"requesturl=%@", requesturl);
NSURL *theURL = [NSURL URLWithString:requesturl];
self.activeDownload = [NSMutableData data];
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:theURL];
[request setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];
[request setTimeoutInterval:60.0];
[request setHTTPMethod:@"POST"];
[request setValue:contentType forHTTPHeaderField: @"Content-Type"]; // DFH you add addValue, I always use setValue
NSLog(@"URL : %@", requesturl);
NSLog(@"REQ : %@", request);
NSLog(@"ContentType \"%@\"", contentType);
if(body)
{
[request setHTTPBody:body];
}
NSLog(@"AllFields : %@", [request allHTTPHeaderFields]);
NSLog(@"HTTPBody : %@", [[NSString alloc] initWithData:[request HTTPBody] encoding:NSUTF8StringEncoding]);
NSLog(@"HTTPMethod : %@", [request HTTPMethod]);
conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];
assert(conn);
}