Objective c 如何调试从iPhone应用程序发送的HTTP POST?
我正在使用以下代码将图像上载到我的服务器:Objective c 如何调试从iPhone应用程序发送的HTTP POST?,objective-c,http,Objective C,Http,我正在使用以下代码将图像上载到我的服务器: // Dictionary that holds post parameters. You can set your post parameters that your server accepts or programmed to accept. NSMutableDictionary* _params = [[NSMutableDictionary alloc] init]; DataManager *manager = [DataManager
// Dictionary that holds post parameters. You can set your post parameters that your server accepts or programmed to accept.
NSMutableDictionary* _params = [[NSMutableDictionary alloc] init];
DataManager *manager = [DataManager sharedManager];
[_params setObject:[manager token] forKey:@"token"];
[_params setObject:[NSString stringWithFormat:@"%d",[manager site_id]] forKey:@"site"];
NSString *BoundaryConstant = @"----------V2ymHFg03ehbqgZCaKO6jy";
NSString* FileParamConstant = @"image";
NSURL* requestURL = [NSURL URLWithString:@"https://mysite.com/api/upload/"];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];
[request setHTTPShouldHandleCookies:NO];
[request setTimeoutInterval:30];
[request setHTTPMethod:@"POST"];
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", BoundaryConstant];
[request setValue:contentType forHTTPHeaderField: @"Content-Type"];
NSMutableData *body = [NSMutableData data];
for (NSString *param in _params) {
[body appendData:[[NSString stringWithFormat:@"--%@\r\n", BoundaryConstant] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"\r\n\r\n", param] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"%@\r\n", [_params objectForKey:param]] dataUsingEncoding:NSUTF8StringEncoding]];
}
NSData *imageData = UIImageJPEGRepresentation(saveImage, 1.0);
if (imageData) {
[body appendData:[[NSString stringWithFormat:@"--%@\r\n", BoundaryConstant] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"; filename=\"image.jpg\"\r\n", FileParamConstant] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Type: image/jpeg\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:imageData];
[body appendData:[[NSString stringWithFormat:@"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
}
[body appendData:[[NSString stringWithFormat:@"--%@--\r\n", BoundaryConstant] dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPBody:body];
NSLog(@"%@",body);
NSString *postLength = [NSString stringWithFormat:@"%d", [body length]];
NSLog(@"Sending request with length: %@",postLength);
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setURL:requestURL];
NSURLConnection *conn = [[NSURLConnection alloc]initWithRequest:request delegate:self];
在我尝试上传时查看我的服务器日志,我没有得到很多信息:
82.132.x.x - - [06/Feb/2013:15:37:45 +0000] "-" 400 0 "-" "-"
将此查询与来自同一应用程序的其他查询进行比较:
82.132.x.x - - [06/Feb/2013:15:36:13 +0000] "GET /api/sites/?token=blah HTTP/1.1" 200 269 "-" "My%20App/1.0 CFNetwork/609.1.4 Darwin/13.0.0"
我怎样才能知道出了什么问题?显然,请求在某种程度上存在错误,导致错误400。有什么工具可以用来检查吗?Wireshark应该允许您检查发送/接收的内容。Wireshark应该允许您检查发送/接收的内容。试试。。。使用它比wireshark更友好(不是免费的…但他们有试用版)试试看。。。使用它比wireshark更友好(不是免费的…但是他们有试用版)我看不到
内容长度:
在你的代码中的任何地方。@paulbailey底部有4行…哦!我想第二次使用Charles。我看不到您的代码中任何地方的内容长度:。@paulbailey底部有4行……噢!我支持Charles。我同意,如果你想做一些专题监控,它可以减少wireshark中的很多噪音。可以在手机或我的(无头Ubuntu)服务器上使用它吗?两者都不能使用。。。但是如果你在iPhone模拟器中运行这个程序,你应该能够在Mac上运行它。在你的ubuntu服务器上,如果你启用了日志功能,你可以监视你传入的http请求,或者使用tcp转储查看我不能使用模拟器而不编写一堆代码,因为这个应用程序正在上传照片。只需在你的开发机器上安装Charles,然后将该机器设置为手机上的HTTP代理。我用这个工作流调试了一个应用商店应用程序中的网络流量,这个应用商店应用程序不是我的。我同意,如果你想做一些主题监控,它可以在wireshark中消除很多噪音。可以在手机或我的(无头Ubuntu)服务器上使用吗?不能在任何一个上使用。。。但是如果你在iPhone模拟器中运行这个程序,你应该能够在Mac上运行它。在你的ubuntu服务器上,如果你启用了日志功能,你可以监视你传入的http请求,或者使用tcp转储查看我不能使用模拟器而不编写一堆代码,因为这个应用程序正在上传照片。只需在你的开发机器上安装Charles,然后将该机器设置为手机上的HTTP代理。我用这个工作流调试了一个不是我的应用商店应用程序的网络流量。因为我可以简单地直接从我的应用程序中转储HTTP正文,这看起来有些过分了。有没有什么地方可以让我粘贴数据,并获得关于它可能失败原因的反馈?wireshark的过滤器,可以更容易地过滤出你不感兴趣的内容-因为我可以直接从我的应用程序中转储HTTP正文,这似乎有些过分了。有没有什么地方可以让我粘贴数据,并获得关于它可能失败原因的反馈?wireshark的过滤器,可以更容易地过滤出您不感兴趣的内容-