[英]How send text to HTTP POST?
In my app I am using this code for sending parameters name, email, website url and comment in blog...Probably some value are wrong setting...Someone can help me solved? 在我的应用程序中,我正在使用此代码发送参数名称,电子邮件,网站url和博客中的注释...可能某些值设置错误...有人可以帮助我解决吗? I'm going crazy!!
我要疯了!! Thanks in advance!
提前致谢!
-(void)invia{
[self.connessione cancel];
NSURL *indirizzo = [NSURL URLWithString:@"http://*********ina.altervista.org/********/feed/"];
//initialize a request from url
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[indirizzo standardizedURL]];
[request setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];
[request setHTTPShouldHandleCookies:NO];
[request setHTTPMethod:@"POST"];
NSDictionary *parametri = [NSDictionary dictionaryWithObjectsAndKeys:
campoSito.text, @"url",
campoNome.text, @"author",
campoEmail.text, @"email",
campoCommento.text, @"content", nil];
NSString *dati_postati=[NSString stringWithFormat:@"%@",parametri];
[request setValue:@"application/x-www-form-urlencoded; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:[dati_postati dataUsingEncoding:NSUTF8StringEncoding]];
//initialize a connection from request
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
self.connessione = connection;
//start the connection
[connection start];
[self dismissViewControllerAnimated:YES completion:nil];
campoCommento.text=nil;
campoEmail.text=nil;
campoSito.text=nil;
campoNome.text=nil;
}
EDIT: 编辑:
@Rich I've tried to read the API documentation for the web service, more precisely the part which concern "Create a Comment on a Post",I think my site ID concern the article is "gnutella"...I do not think there is a numeric id...from which I extract the id? @Rich我尝试阅读该Web服务的API文档,更确切地说,是有关“在帖子上创建评论”的部分,我认为我的站点ID与该文章有关的是“ gnutella” ...我不认为有一个数字编号...我从中提取编号吗?
UPDATE: 更新:
I've extract Post ID from article...is 1757 I've tried with this URL without success 我从文章中提取了帖子ID ...是1757我尝试使用此URL失败了
NSURL *indirizzo = [NSURL URLWithString:@"http://zenzeroincucina.altervista.org/gnutella/1757/"];
NSURL *indirizzo = [NSURL URLWithString:@"http://zenzeroincucina.altervista.org/gnutella/1757/replies/new"];
NSURL *indirizzo = [NSURL URLWithString:@"http://zenzeroincucina.altervista.org/wp-admin/post.php?post=1757&action=edit"];
Another code method: 另一种编码方法:
#define kSendCommentJSON @"?json=respond.submit_comment"
NSURL *completeURL = [NSURL URLWithString:[NSString stringWithFormat:@"http://zenzeroincucina.altervista.org/gnutella/feed/"]];
AFHTTPClient *httpClient = [AFHTTPClient clientWithBaseURL:completeURL];
NSDictionary *parametri = [NSDictionary dictionaryWithObjectsAndKeys:
campoSito.text, @"url",
campoNome.text, @"author",
campoEmail.text, @"email",
campoCommento.text, @"content", nil];
NSMutableURLRequest *request = [httpClient multipartFormRequestWithMethod:@"POST" path:@"" parameters:parametri constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
//
}];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"Completed Successfullly");
//[self commentPostSuccess];
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Failed misurable %@", [error description]);
//[self commentPostFailed];
}];
[operation start];
I've tried with this method...not yet!! 我已经尝试过这种方法...还没有! Console return successfully but text is not sent correctly... Also tested on device(for exclusion a possible bug)...nothing!
控制台成功返回,但文本未正确发送...也在设备上进行了测试(排除可能的错误)...没有!
You need to send the parameters correctly in the format key=value&key=value
, not just as description
of a NSDictionary
. 您需要以
key=value&key=value
的格式正确发送参数,而不仅仅是NSDictionary
description
。
NSDictionary *parametri = [NSDictionary dictionaryWithObjectsAndKeys:
campoSito.text, @"url",
campoNome.text, @"author",
campoEmail.text, @"email",
campoCommento.text, @"content", nil];
NSMutableArray *values = [NSMutableArray new];
[parametri enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
// Create an encoded parameter pair -> k=v
NSString *p = [NSString stringWithFormat:@"%@=%@", key, [[obj description] stringByAddingPercentEscapesUsingEncoding:NSUTF8Encoding]];
[values addObject:p];
}];
NSString *dati_postati = [values componentsJoinedByString:@"&"];
UPDATE: 更新:
So after looking at the POST
request that is sent (on this page: 因此,在查看发送的
POST
请求之后(在此页面上:
HTTP POST http://zenzeroincucina.altervista.org/wp-comments-post.php
Host: zenzeroincucina.altervista.org
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.9; rv:28.0) Gecko/20100101 Firefox/28.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-gb,en;q=0.5
Accept-Encoding: gzip, deflate
Referer: http://zenzeroincucina.altervista.org/gnutella/
Cookie: __cfduid=d1ef25815caa898a777114c04c4d37bfc1398261681664; av_device_cookie=computer; av_mobile_cookie=desktop; PHPSESSID=b27e99a06qrkkd2lenppt9p1i6; __utma=178781179.1711693902.1398262580.1398262580.1398262580.1; __utmc=178781179; __utmz=178781179.1398262580.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none)
Connection: keep-alive
Content-Type: application/x-www-form-urlencoded
Content-Length: 201
author=Test&email=test%40stack.com&url=stackoverflow.com&comment=Test&captcha_code=dw3P&submit=Submit+Comment&comment_post_ID=1757&comment_post_ID=1757&comment_parent=0&akismet_comment_nonce=f8393add8f
This doesn't massively help however as you need to include the captcha . 但是,这并没有太大帮助,因为您需要包含验证码 。
What you want to use is the WordPress API for posting comments. 您要使用的是用于发布评论的WordPress API 。 I played around with this but I don't have your site ID.
我玩过这个游戏,但没有您的网站ID。
Try this out: 试试看:
-(void)invia{
[self.connessione cancel];
NSString *param = [NSString stringWithFormat:@"url=%@&author=%@&email=%@&content=%@",campoSito.text,campoNome.text,campoEmail.text,campoCommento.text];
NSURL *indirizzo = [NSURL URLWithString:[NSString stringWithFormat:@"http://*********ina.altervista.org/********/feed?%@",param]];
//initialize a request from url
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[indirizzo standardizedURL]];
[request setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];
[request setHTTPShouldHandleCookies:NO];
[request setHTTPMethod:@"POST"];
NSString *dati_postati=[NSString stringWithFormat:@"%@",parametri];
[request setValue:@"application/x-www-form-urlencoded; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:[dati_postati dataUsingEncoding:NSUTF8StringEncoding]];
//initialize a connection from request
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
self.connessione = connection;
//start the connection
[connection start];
[self dismissViewControllerAnimated:YES completion:nil];
campoCommento.text=nil;
campoEmail.text=nil;
campoSito.text=nil;
campoNome.text=nil;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.