2014-06-09 2 views
0

iOS 7/8에서 HTTP POST를 사용하여 내 서버로 사진을 보내려고합니다. 내 사이트의 어떤 URL이 설정되었는지에 관계없이 요청에 오류가 발생합니다 (아래 붙여 넣기). 나는 호스팅 회사에 전자 메일을 보냈고 그들은 나쁜 헤더로 인한 것이라고 말했다. 내 코드에 무슨 문제가 있는지 알 수 없다 ... 웹에서 코드 예제와 동일한 경계 문자열을 넣었으므로,하지 마라.NSMutableURLRequest error 잘못된 헤더

제발 도와주세요!

라이브 코드 : 여기

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

누군가가 내 사과를 사과 데 보홀에서 발견했습니다. 내 코드를 확인하면 "form"이 아닌 "from"을 "from"이라고 입력했습니다. 작은 실수가 있습니다. ^^ '

고마워요!

관련 문제