gpt4 book ai didi

iphone - 使用 Twitpic API 上传图像时出现错误的响应代码

转载 作者:行者123 更新时间:2023-12-03 21:24:45 26 4
gpt4 key购买 nike

任何熟悉使用“Twitpic api”将图像上传到 Twitter 的源代码的人都可以告诉我为什么当我尝试上传图像时收到 0 响应代码吗?

这是我的代码:

- (BOOL)uploadImageToTwitpic:(UIImage*)image
withMessage:(NSString*)theMessage
username:(NSString*)username
password:(NSString*)password
{
NSString *stringBoundary, *contentType, *message, *baseURLString, *urlString;
NSData *imageData;
NSURL *url;
NSMutableURLRequest *urlRequest;
NSMutableData *postBody;

// Create POST request from message, imageData, username and password
baseURLString = kTwitpicUploadURL;
urlString = [NSString stringWithFormat:@"%@", baseURLString];
url = [NSURL URLWithString:urlString];
urlRequest = [[[NSMutableURLRequest alloc] initWithURL:url] autorelease];
[urlRequest setHTTPMethod:@"POST"];

// Set the params
message = ([theMessage length] > 1) ? theMessage : @"Here's my new Light Table collage.";
imageData = UIImageJPEGRepresentation(image, kTwitpicImageJPEGCompression);

// Setup POST body
stringBoundary = [NSString stringWithString:@"0xKhTmLbOuNdArY"];
contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", stringBoundary];
[urlRequest addValue:contentType forHTTPHeaderField:@"Content-Type"];

// Setting up the POST request's multipart/form-data body
postBody = [NSMutableData data];
[postBody appendData:[[NSString stringWithFormat:@"\r\n\r\n--%@\r\n", stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"source\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithString:@"lighttable"] dataUsingEncoding:NSUTF8StringEncoding]]; // So Light Table show up as source in Twitter post

[postBody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"username\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithString:username] dataUsingEncoding:NSUTF8StringEncoding]]; // username

[postBody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"password\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithString:password] dataUsingEncoding:NSUTF8StringEncoding]]; // password

[postBody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"message\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithString:message] dataUsingEncoding:NSUTF8StringEncoding]]; // message

[postBody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"media\"; filename=\"%@\"\r\n", @"lighttable_twitpic_image.jpg" ] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithString:@"Content-Type: image/jpg\r\n"] dataUsingEncoding:NSUTF8StringEncoding]]; // jpeg as data
[postBody appendData:[[NSString stringWithString:@"Content-Transfer-Encoding: binary\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:imageData]; // Tack on the imageData to the end

[postBody appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n", stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
[urlRequest setHTTPBody:postBody];
NSLog(@"data=======>%@",postBody);
NSLog(@"URLReq========>%@",urlRequest);
// Spawn a new thread so the UI isn't blocked while we're uploading the image
[NSThread detachNewThreadSelector:@selector(uploadingDataWithURLRequest:) toTarget:self withObject:urlRequest];

return YES; // TODO: Should raise exception on error
}

- (void)uploadingDataWithURLRequest:(NSURLRequest*)urlRequest {
// Called on a separate thread; upload and handle server response

NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

[urlRequest retain]; // Retain since we autoreleased it before

// Send the request
NSHTTPURLResponse *urlResponse;
NSError *error;
NSData *responseData = [NSURLConnection sendSynchronousRequest:urlRequest
returningResponse:&urlResponse
error:&error];
NSString *responseString = [[NSString alloc] initWithData:responseData
encoding:NSUTF8StringEncoding];

// Handle the error or success
// If error, create error message and throw up UIAlertView
NSLog(@"Response Code: %d", [urlResponse statusCode]);
if ([urlResponse statusCode] >= 200 && [urlResponse statusCode] < 300) {
NSLog(@"urlResultString: %@", responseString);

NSString *match = [responseString stringByMatching:@"http[a-zA-Z0-9.:/]*"]; // Match the URL for the twitpic.com post
NSLog(@"match: %@", match);

// Send back notice to delegate
[delegate twitpicEngine:self didUploadImageWithResponse:match];
}
else {
NSLog(@"Error while uploading, got 400 error back or no response at all: %@", [urlResponse statusCode]);
[delegate twitpicEngine:self didUploadImageWithResponse:nil]; // Nil should mean "upload failed" to the delegate
}

[pool drain];
[responseString release];
[urlRequest release];
}

最佳答案

编辑:我建议阅读 this SO question看看创建 POST 请求的不同方式是否有帮助。

<小时/>

我不熟悉 Twitpic API我自己,但我会尝试提出一些建议来帮助您缩小问题范围。

我要检查的第一件事是 POST 正文的正确性。您的创建代码不必要地复杂且难以阅读,因此如果其中可能存在错误,我不会感到惊讶。我正在发布一个修订版本(免责声明,我还没有编译它),它可以简化创建并提高性能。 (您创建了大量自动释放的 NSString 和 NSData 对象,并且每次都将字符串转换为数据只是为了附加数据字节。构建可变字符串并转换一次是一种更快、更简单的方法。)

与此相关的是,当每个变量都在方法顶部声明时,通读代码会有点困难。这在任何最新的 C(或派生语言)标准中都是不必要的,并且在第一次使用变量时声明变量被认为是更好的做法。它不仅使代码更易于阅读,而且通常会删除一些不必要的行。

这是带有一些建议修改的代码。它们可能会让查明问题变得更加容易。

- (BOOL)uploadImageToTwitpic:(UIImage*)image
withMessage:(NSString*)theMessage
username:(NSString*)username
password:(NSString*)password
{
// Create POST request from message, imageData, username and password
NSString *baseURLString = kTwitpicUploadURL;
NSString *urlString = [NSString stringWithFormat:@"%@", baseURLString];
NSURL *url = [NSURL URLWithString:urlString];
NSMutableURLRequest *urlRequest = [[[NSMutableURLRequest alloc] initWithURL:url] autorelease];
[urlRequest setHTTPMethod:@"POST"];

// Set the params
NSString *message = ([theMessage length] > 1) ? theMessage : @"Here's my new Light Table collage.";

// Setup POST body
NSString *stringBoundary = [NSString stringWithString:@"0xKhTmLbOuNdArY"];
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", stringBoundary];
[urlRequest addValue:contentType forHTTPHeaderField:@"Content-Type"];

NSString *stringBoundarySeparator = [NSString stringWithFormat:@"\r\n--%@\r\n", stringBoundary];

NSMutableString *postString = [NSMutableString string];
[postString appendString:@"\r\n"];
[postString appendString:stringBoundarySeparator];
[postString appendString:@"Content-Disposition: form-data; name=\"source\"\r\n\r\n"];
[postString appendString:@"lighttable"]; // So Light Table shows up as source in Twitter
[postString appendString:stringBoundarySeparator];
[postString appendStringWithFormat:@"Content-Disposition: form-data; name=\"username\"\r\n\r\n%@", username];
[postString appendString:stringBoundarySeparator];
[postString appendStringWithFormat:@"Content-Disposition: form-data; name=\"password\"\r\n\r\n%@", password];
[postString appendString:stringBoundarySeparator];
[postString appendStringWithFormat:@"Content-Disposition: form-data; name=\"message\"\r\n\r\n%@", message];
[postString appendString:stringBoundarySeparator];
[postString appendStringWithFormat:@"Content-Disposition: form-data; name=\"media\"; filename=\"%@\"\r\n", @"lighttable_twitpic_image.jpg"];
[postString appendString:@"Content-Type: image/jpg\r\n"]; // data as JPEG
[postString appendString:@"Content-Transfer-Encoding: binary\r\n\r\n"];

// Setting up the POST request's multipart/form-data body
NSMutableData *postBody = [NSMutableData data];
[postBody appendData:[postString dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:UIImageJPEGRepresentation(image, kTwitpicImageJPEGCompression)]; // Tack on the image data to the end
[postBody appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n", stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];

[urlRequest setHTTPBody:postBody];
NSLog(@"data=======>%@",postBody);
NSLog(@"URLReq========>%@",urlRequest);
// Spawn a new thread so the UI isn't blocked while we're uploading the image
[NSThread detachNewThreadSelector:@selector(uploadingDataWithURLRequest:) toTarget:self withObject:urlRequest];

return YES; // TODO: Should raise exception on error
}

// Called on a separate thread; upload and handle server response
- (void)uploadingDataWithURLRequest:(NSURLRequest *)urlRequest {
[urlRequest retain]; // Retain since we're using it in this method

NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

// Send the request
NSHTTPURLResponse *urlResponse;
NSError *error;
NSData *responseData = [NSURLConnection sendSynchronousRequest:urlRequest
returningResponse:&urlResponse
error:&error];
NSString *responseString = [[[NSString alloc] initWithData:responseData
encoding:NSUTF8StringEncoding] autorelease];

// Handle the error or success
// If error, create error message and throw up UIAlertView
NSLog(@"Response Code: %d", [urlResponse statusCode]);
NSLog(@"Response String: %@", responseString);
if ([urlResponse statusCode] >= 200 && [urlResponse statusCode] < 300) {
NSString *match = [responseString stringByMatching:@"http[a-zA-Z0-9.:/]*"]; // Match the URL for the twitpic.com post
NSLog(@"match: %@", match);
// Send back notice to delegate
[delegate twitpicEngine:self didUploadImageWithResponse:match];
}
else {
NSLog(@"Error while uploading, got 400 error back or no response at all: %@", [urlResponse statusCode]);
[delegate twitpicEngine:self didUploadImageWithResponse:nil]; // Nil should mean "upload failed" to the delegate
}
[pool drain];
[urlRequest release];
}

有一件事可以帮助我们回答您的问题:您声明响应代码为 0,但没有说明响应的其余部分(您在 responseString 中记录的)是什么。由于您仅打印状态代码在 [200,300) 范围内的情况,因此您可能看不到问题的原因,即使 Twitpic可能 将其传递回给您。值得一试...(我已经在上面的代码中做到了这一点。)

关于iphone - 使用 Twitpic API 上传图像时出现错误的响应代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1188094/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com