AFNetworking文件上传适用于我的PHP脚本,但不适用于jsp脚本

时间:2011-11-07 18:52:27

标签: objective-c afnetworking

我正在编写一个iPhone应用程序,它将一些信息(一些文本和图像)发送到服务器脚本。

使用我的php脚本一切正常,但是我的客户的jsp脚本(将收到的信息发送到电子邮件地址的电子邮件)没有任何作用...(我知道因为我应该收到自动回复电子邮件,这不会'发生)。

我无法访问我的客户的脚本,因为他们是一个大客户和“一些bla bla安全问题......” 该脚本与在线Web表单一起正常工作,并且在iOS 4.3上与ASIHTTPRequest一起运行良好(我正在迁移到AFNetworking,因为iOS 5 ASIHTTPRequest不起作用)

任何帮助都非常感谢!!!

这是我的代码...请注意我的代码总是到达NSLog(@“成功”),但我没有收到任何自动回复电子邮件,所以它不能正常工作!!

AFHTTPClient *client= [AFHTTPClient clientWithBaseURL:[NSURL URLWithString:@"http://my.client.server.com"]];


    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    [parameters setObject:[fieldName text] forKey:@"field01_nome"];
    [parameters setObject:[fieldSurname text] forKey:@"field02_cognome"];



    NSMutableURLRequest *myRequest = [client multipartFormRequestWithMethod:@"POST" path:@"/Contents/mail/sendToForm.jsp" parameters:parameters constructingBodyWithBlock: ^(id <AFMultipartFormData>formData) {
        [formData appendPartWithFileData:myNSDataToSend mimeType:@"image/jpeg" name:@"alleagto"];
    }];


    AFHTTPRequestOperation *operation = [AFHTTPRequestOperation HTTPRequestOperationWithRequest:myRequest success:^(id object) {
        NSLog(@"Success");

    } failure:^(NSHTTPURLResponse *response, NSError *error) {
        NSLog(@"Fail");

    }];


    [operation setUploadProgressBlock:^(NSInteger bytesWritten, NSInteger totalBytesWritten, NSInteger totalBytesExpectedToWrite) {
        NSLog(@"Sent %d of %d bytes", totalBytesWritten, totalBytesExpectedToWrite);

    }];

    queue = [[[NSOperationQueue alloc] init] autorelease];
    [queue addOperation:operation];

非常感谢!!

编辑:正如JosephH所建议的那样,我添加了捕获的请求:

从在线Web表单生成的请求(始终有效):

POST /Contents/mail/sendToForm.jsp HTTP/1.1
Host: my.client.server.com
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_2) AppleWebKit/534.51.22 (KHTML, like Gecko) Version/5.1.1 Safari/534.51.22
Content-Length: 54246
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8  */<- added by me for syntax coloring
Origin: http://www.client.server.com
Content-Type: multipart/form-data; boundary=----WebKitFormBoundaryLWVo7zrkX3QOybWm
Referer: http://www.client.server.com/form/1/send-the-form.shtml
Accept-Language: it-it
Accept-Encoding: gzip, deflate
Pragma: no-cache
Connection: keep-alive

------WebKitFormBoundaryLWVo7zrkX3QOybWm
Content-Disposition: form-data; name="form_recipient"

4182fd0f318e1d86176a3be7f8c7d83955eb5eca2e1402250e47164caf1984e7
------WebKitFormBoundaryLWVo7zrkX3QOybWm
Content-Disposition: form-data; name="form_subject"


------WebKitFormBoundaryLWVo7zrkX3QOybWm
Content-Disposition: form-data; name="field01_nome"

Gianluca
------WebKitFormBoundaryLWVo7zrkX3QOybWm
Content-Disposition: form-data; name="field02_cognome"

Pisati
------WebKitFormBoundaryLWVo7zrkX3QOybWm
Content-Disposition: form-data; name="field03_indirizzo"

A
------WebKitFormBoundaryLWVo7zrkX3QOybWm
Content-Disposition: form-data; name="field04_provincia"

B
------WebKitFormBoundaryLWVo7zrkX3QOybWm
Content-Disposition: form-data; name="field05_email"

my_email@gmail.com
------WebKitFormBoundaryLWVo7zrkX3QOybWm
Content-Disposition: form-data; name="field06_telefono"

333
------WebKitFormBoundaryLWVo7zrkX3QOybWm
Content-Disposition: form-data; name="field07_tipo"

valore2
------WebKitFormBoundaryLWVo7zrkX3QOybWm
Content-Disposition: form-data; name="allegato"; filename="allegato.jpg"
Content-Type: image/jpeg

Here there's the attachment

------WebKitFormBoundaryLWVo7zrkX3QOybWm
Content-Disposition: form-data; name="field09_oggetto"

Test Web
------WebKitFormBoundaryLWVo7zrkX3QOybWm
Content-Disposition: form-data; name="field10_segnalazione"

Send Test Web
------WebKitFormBoundaryLWVo7zrkX3QOybWm--

使用ASIHTTP从iOS 5.0生成的请求(我不知道为什么,但有时它可以工作,其他时候没有...)

POST /Contents/mail/sendToForm.jsp HTTP/1.1
Host: my.client.server.com
User-Agent: My App 1.0 (iPhone Simulator; iPhone OS 5.0; en_US)
Content-Length: 153522
Content-Type: multipart/form-data; charset=utf-8; boundary=0xKhTmLbOuNdArY-ABC90FA4-3EE3-42FE-B93A-A698D7BB894D
Accept-Encoding: gzip
Connection: close

--0xKhTmLbOuNdArY-ABC90FA4-3EE3-42FE-B93A-A698D7BB894D
Content-Disposition: form-data; name="field01_nome"

Gianluca
--0xKhTmLbOuNdArY-ABC90FA4-3EE3-42FE-B93A-A698D7BB894D
Content-Disposition: form-data; name="field02_cognome"

Pisati
--0xKhTmLbOuNdArY-ABC90FA4-3EE3-42FE-B93A-A698D7BB894D
Content-Disposition: form-data; name="field03_indirizzo"

A
--0xKhTmLbOuNdArY-ABC90FA4-3EE3-42FE-B93A-A698D7BB894D
Content-Disposition: form-data; name="field04_provincia"

B
--0xKhTmLbOuNdArY-ABC90FA4-3EE3-42FE-B93A-A698D7BB894D
Content-Disposition: form-data; name="field05_email"

gianluca.pisati@gmail.com
--0xKhTmLbOuNdArY-ABC90FA4-3EE3-42FE-B93A-A698D7BB894D
Content-Disposition: form-data; name="field06_telefono"

333
--0xKhTmLbOuNdArY-ABC90FA4-3EE3-42FE-B93A-A698D7BB894D
Content-Disposition: form-data; name="field07_tipo"

valore2
--0xKhTmLbOuNdArY-ABC90FA4-3EE3-42FE-B93A-A698D7BB894D
Content-Disposition: form-data; name="field09_oggetto"

Test iOS 5
--0xKhTmLbOuNdArY-ABC90FA4-3EE3-42FE-B93A-A698D7BB894D
Content-Disposition: form-data; name="field10_segnalazione"

ASIHTTP on iOS 5
--0xKhTmLbOuNdArY-ABC90FA4-3EE3-42FE-B93A-A698D7BB894D
Content-Disposition: form-data; name="form_recipient"

4182fd0f318e1d86176a3be7f8c7d83955eb5eca2e1402250e47164caf1984e7
--0xKhTmLbOuNdArY-ABC90FA4-3EE3-42FE-B93A-A698D7BB894D
Content-Disposition: form-data; name="form_subject"


--0xKhTmLbOuNdArY-ABC90FA4-3EE3-42FE-B93A-A698D7BB894D
Content-Disposition: form-data; name="allegato"; filename="image.jpg"
Content-Type: image/jpeg

Here there's the attachment

--0xKhTmLbOuNdArY-ABC90FA4-3EE3-42FE-B93A-A698D7BB894D--

使用AFNetworking从iOS 5.0生成的请求(从不起作用):

POST /Contents/mail/sendToForm.jsp HTTP/1.1
Host: my.client.server.com
User-Agent: com.mycompany.myapp/1.0 (unknown, iPhone OS 5.0, iPhone Simulator, Scale/1.000000)
Content-Length: 153203
Accept: */*                     */<- added by me for syntax coloring
Accept-Language: en, fr, de, ja, nl, it, es, pt, pt-PT, da, fi, nb, sv, ko, zh-Hans, zh-Hant, ru, pl, tr, uk, ar, hr, cs, el, he, ro, sk, th, id, ms, en-GB, ca, hu, vi, en-us;q=0.8
Accept-Encoding: gzip
Content-Type: multipart/form-data; boundary=Boundary+0xAbCdEfGbOuNdArY
Connection: keep-alive


--Boundary+0xAbCdEfGbOuNdArY
Content-Disposition: form-data; name="form_recipient"

4182fd0f318e1d86176a3be7f8c7d83955eb5eca2e1402250e47164caf1984e7
--Boundary+0xAbCdEfGbOuNdArY
Content-Disposition: form-data; name="field04_provincia"

B
--Boundary+0xAbCdEfGbOuNdArY
Content-Disposition: form-data; name="field05_email"

gianluca.pisati@gmail.com
--Boundary+0xAbCdEfGbOuNdArY
Content-Disposition: form-data; name="field07_tipo"

valore2
--Boundary+0xAbCdEfGbOuNdArY
Content-Disposition: form-data; name="field01_nome"

Gianluca
--Boundary+0xAbCdEfGbOuNdArY
Content-Disposition: form-data; name="field10_segnalazione"

AFNetworking on iOS 5
--Boundary+0xAbCdEfGbOuNdArY
Content-Disposition: form-data; name="field02_cognome"

Pisati
--Boundary+0xAbCdEfGbOuNdArY
Content-Disposition: form-data; name="form_subject"


--Boundary+0xAbCdEfGbOuNdArY
Content-Disposition: form-data; name="field06_telefono"

333
--Boundary+0xAbCdEfGbOuNdArY
Content-Disposition: form-data; name="field09_oggetto"

Test iOS 5
--Boundary+0xAbCdEfGbOuNdArY
Content-Disposition: form-data; name="field03_indirizzo"

A
--Boundary+0xAbCdEfGbOuNdArY
Content-Disposition: form-data; name="allegato"; filename="allegato-342447627.jpeg"
Content-Type: image/jpeg

Here there's the attachment

--Boundary+0xAbCdEfGbOuNdArY--

2 个答案:

答案 0 :(得分:0)

ASIHTTPRequest在iOS 5.0上运行良好;使用ASIHTTPRequest的100个应用程序在iOS 5上没有任何问题。

也就是说,从长远来看,转向AFNetworking可能是有道理的。

这些“它在浏览器中工作,但没有”类型问题很难通过代码检查解决 - 最简单的解决方法是安装像charlesproxy这样的工具。

安装完成后,开始捕获,然后在浏览器中执行请求,验证它是否成功,然后从iOS模拟器执行。然后将浏览器的请求与AFNetworking的请求进行比较,看看您可以发现哪些差异。

如果您无法弄明白,请编辑您的问题以包含两个捕获的请求,我相信有人能够发现它。

答案 1 :(得分:0)

AFNetworking和multipart / form-data也存在问题。解决了。当AFNetworking为multipart / form-data生成HTTP主体时,它会将\ r \ n sumbols放在beginig上。您的AFNetworking机构与其他机构之间的区别 - 在开始时是\ r \ n。尝试删除它。我修改了一些AFHTTPClient.m方法,将它们从body的开头删除:

static inline NSString * AFMultipartFormEncapsulationBoundary() {
    return [NSString stringWithFormat:@"%@--%@%@", @""/*kAFMultipartFormLineDelimiter*/, kAFMultipartFormBoundary, kAFMultipartFormLineDelimiter];

}

- (void)appendPartWithHeaders:(NSDictionary *)headers body:(NSData *)body {
    [self appendString:AFMultipartFormEncapsulationBoundary()];

    for (NSString *field in [headers allKeys]) {
        [self appendString:[NSString stringWithFormat:@"%@: %@%@", field, [headers valueForKey:field], kAFMultipartFormLineDelimiter]];
    }

    [self appendString:kAFMultipartFormLineDelimiter];
    [self appendData:body];
    [self appendString:kAFMultipartFormLineDelimiter];

}

它对我有用