2014-06-09 166 views
0

我正尝试在iOS 7/8上使用HTTP POST向我的服务器发送图片。 我的问题是,无论我的网站设置了哪个网址,我的请求都会出现错误(粘贴在下面)。 我发送了一封电子邮件给我的托管公司,他们告诉我说这是由于一个糟糕的标题。我无法弄清楚我的代码出了什么问题......我把相同的边界字符串放在网络上的代码示例中,mabe应该没有做到。NSMutableURLRequest错误错误标题

请帮助我!

直播代码:

NSData *imageData = UIImageJPEGRepresentation([UIImage imageNamed:@"uploadingpic.png"], 90); 
    self.preview.image = [UIImage imageNamed:@"uploadingpic.png"]; 
    NSString *[email protected]"http://myCoolURL/upload.php"; 

    NSMutableURLRequest *request=[[NSMutableURLRequest alloc]init]; 
    [request setURL:[NSURL URLWithString:urlString]]; 
    [request setHTTPMethod:@"POST"]; 

    NSString *boundary = [NSString stringWithString:@"---------------------------14737809831466499882746641449"]; 
    NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary]; 
    [request addValue:contentType forHTTPHeaderField:@"Content-Type"]; 

    NSMutableData *body = [NSMutableData data]; 
    [body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]]; 
    [body appendData:[[NSString stringWithString:@"Content-Disposition: from-data; name=\"userfile\"; filename=\".jpg\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]]; 
    [body appendData:[[NSString stringWithString:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]]; 
    [body appendData:[NSData dataWithData:imageData]]; 
    [body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]]; 
    [request setHTTPBody:body]; 

    NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil]; 
    NSString *returnString=[[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding]; 

    NSLog(returnString); 

下面是详细的错误(具体以我servor提供商,我认为)返回的NSLog:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> 
<html lang="fr"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 
<TITLE>400 Bad Request</TITLE> 
</HEAD> 
<BODY> 
<H1>Bad Request</H1> 
Your browser sent a request that this server could not understand. 
<P> 
Client sent malformed Host header 
<P> 
<HR> 
<H1>Mauvaise Requête</H1> 
Votre navigateur a envoyé une demande que ce serveur ne peut pas comprendre. 
<P> 
Le client a envoyé une en tête malformé 
<P> 
<HR> 
<H1>Solicitud incorrecta</H1> 
Su navegador ha enviado una solicitud que el servidor no puede entender. 
<P> 
El cliente ha enviado un encabezado incorrecto. 
<P> 
<HR> 
<ADDRESS> 
</ADDRESS> 
</BODY> 
</HTML> 

<!-- 
    - Unfortunately, Microsoft has added a clever new 
    - "feature" to Internet Explorer. If the text of 
    - an error's message is "too small", specifically 
    - less than 512 bytes, Internet Explorer returns 
    - its own error message. You can turn that off, 
    - but it's pretty tricky to find switch called 
    - "smart error messages". That means, of course, 
    - that short error messages are censored by default. 
    - IIS always returns error messages that are long 
    - enough to make Internet Explorer happy. The 
    - workaround is pretty simple: pad the error 
    - message with a big comment like this to push it 
    - over the five hundred and twelve bytes minimum. 
    - Of course, that's exactly what you're reading 
    - right now. 
    --> 
+1

这里可能有一个错字:Content-Disposition:from-data; ...你想要表单数据吗? – gro

回答

0

我没有准备好回答您的问题但是如果你仔细观察你发送的头文件与HTML中的上传文件非常相似,因为基本上就是这样。如果您正在从应用程序中调试这些类型的问题,那么您会浪费大量时间反复重新启动仿真器/调试设备。

我建议您尝试使用简单的上传表单首先修复上传,然后张贴到动态页面。你运行什么样的服务器还不清楚,但我想它有PHP支持。因此,创建form.html并让其发布到file.php。确保工作。如果这不起作用,你知道托管公司有一些问题。

下一步是找出它使用Firebug或更好的网络调试工具发送的头文件。表单提交的工作标题总是工作标题,因为接收脚本文件不关心它来自哪里。这些标题也可以在您的应用中运行。

我在服务器上看到的一件事情是,它们没有使用相同的字符串编码。当你没有自己指定任何编码时,你可以通过检出文本文档(如HTML页面)来找出字符串编码的内容。 UTF8应该没问题,但我不得不在某些场合诉诸Windows1252。设想一些重要的标题字段有损坏的特殊字符,这是行不通的。

0

有人发现我的苹果devforums错误。 如果你检查我的代码,我在某处输入“from”而不是“form” 这样一个小错误^^'

谢谢你!