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.
-->
의 경우 여기에 오타가있을 수 있습니다. Content-Disposition : from-data; ... 양식 데이터를 원하셨습니까? – gro