2011-11-07 32 views
0

我在寫一個iPhone應用程序,它向服務器腳本發送一些信息(一些文本和圖像)。AFNetworking文件上傳與我的PHP腳本一起工作,但不能用於jsp腳本

與我的PHP腳本一切正常,但與我的客戶的jsp腳本(發送電子郵件與收到的信息到一個電子郵件地址)沒有任何作品...(我知道因爲我應該收到autoreply電子郵件,這doesn沒有發生)。

我沒有訪問我的客戶的腳本因爲他們是大客戶和「一些喇嘛喇嘛的安全問題......」 腳本正常工作與在線網絡表單,並將其與ASIHTTPRequest行之有效的的iOS 4.3(我正在遷移到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建議我已經添加了拍攝請求:

請求來自在線網絡表單生成(其中工程總):與ASIHTTP從iOS 5.0產生

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" 

[email protected] 
------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-- 

請求(我不知道爲什麼,但有時它的工作原理,其他時間沒有...)與AFNetworking(這永遠不會奏效)從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" 

[email protected] 
--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-- 

請求:

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" 

[email protected] 
--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-- 

回答

0

ASIHTTPRequest在iOS 5.0上工作得很好;使用ASIHTTPRequest的100多個應用程序在iOS 5上沒有問題。

也就是說,轉向AFNetworking從長遠來看可能是合理的。

這些「它在瀏覽器中工作,但沒有」類型問題很難通過代碼檢查來解決 - 解決它們的最簡單方法是安裝一個像charlesproxy這樣的工具。

安裝完成後,開始捕獲,然後在瀏覽器中執行請求,驗證它是否成功,然後從iOS模擬器執行。然後將瀏覽器的請求與AFNetworking的請求進行比較,看看你能發現哪些差異。

如果你不能弄明白,編輯你的問題,包括兩個捕獲的請求,我相信有人能夠發現它。

+0

感謝您的意見對我的作品.. !!唯一改變的是參數的順序,這可能是問題嗎?我不這麼認爲...... – Janky

+0

你確定這是唯一不同的 - 你比較了所有的頭文件嗎?如果這是唯一不同的事情,那肯定是問題所在 - 但我相信會有其他的不同。 – JosephH

+0

好吧,我已經添加了3個捕獲的請求,第一個是來自Web窗體,第二個是使用ASIHTTP(有時與iOS 5無法使用!我不知道爲什麼......),第三個一個是AFNetworking – Janky

0

AFNetworking和multipart/form-data也有問題。解決了。當AFNetworking爲multipart/form-data生成HTTP主體時,它會在beginig處插入\ r \ n符號。您的AFNetworking機構與其他機構之間的區別 - 開始時爲\ r \ n。嘗試刪除它。我修改了一些AFHTTPClient。米的方法,從人體的刪除它們開始:

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]; 

}

相關問題