[英]NSURLRequest POST to google app engine?
我目前正在研究GAE(Python),並且正在嘗試從iPhone應用程序發送POST請求。
使用GET方法發送可以正常工作,但是當我使用POST方法時只會失敗。
這是服務器端:
class echoHandler(webapp.RequestHandler):
def get(self):
password = self.request.get("password")
if password == "ping":
self.response.out.write("pong")
else:
self.response.out.write("erreur de password")
def post(self):
password = self.request.get("password")
if password == "ping":
self.response.out.write("pong")
else:
self.response.out.write("erreur de password")
在客戶端:
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://myapp.appspot.com/echo"]];
NSMutableURLRequest *maRequete = [NSMutableURLRequest requestWithURL:url];
[maRequete setHTTPMethod:@"POST"];
NSString *essaiMdp = @"password=ping";
[maRequete setHTTPBody:[essaiMdp dataUsingEncoding:NSUTF8StringEncoding]];
//[maRequete setValue:[NSString stringWithFormat:@"%d",[essaiMdp length]] forHTTPHeaderField:@"Content-Length"];
//[maRequete setValue:@"text/html" forHTTPHeaderField:@"Content-Type"];
//[maRequete setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"content-type"];
NSURLResponse *reponse;
NSError *erreur;
NSData *donneesPOST = [NSURLConnection sendSynchronousRequest:maRequete returningResponse:&reponse error:&erreur];
if (donneesPOST) {
NSString *strResultat = [[NSString alloc] initWithData:donneesPOST encoding:NSUTF8StringEncoding];
NSLog(@"reponse recue !");
NSLog(strResultat);
resultat.text = strResultat;
}
else {
if (erreur) {
NSLog(@"erreur lors de la requete HTTP:%@", url);
NSLog(@"erreur: %@", [erreur localizedDescription]);
}
}
奇怪的是,一切都可以在localhost中運行,但是當我在Internet上部署應用程序時,POST方法停止了工作。
任何想法 ? 謝謝 !
如果您要向Google App Engine發送POST請求,請將URL更改為“ https”“ http://myapp.appspot.com/echo ”更改為“ https://myapp.appspot.com/echo ”。
根據我的經驗,向Google App Engine發送“非https”網址會導致重定向。
是否有可能將您的服務器設置為將不以斜杠結尾的請求重定向到一個以斜杠結尾的請求? 換句話說,也許http://myapp.appspot.com/echo
重定向到http://myapp.appspot.com/echo/
,並且重定向始終是GET。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.