2014-06-09 161 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」 這樣一個小錯誤^^'

謝謝你!