如何将文本发送到HTTP POST?

时间:2014-04-21 00:14:09

标签: ios http post request comments

在我的应用程序中,我使用此代码在博客中发送参数名称,电子邮件,网站URL和评论...可能有些值设置错误...有人可以帮我解决?我疯了!提前致谢!

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


}

编辑:

@Rich我试图阅读网络服务的API文档,更准确地说是关注的一部分"在帖子上创建评论",我认为我的网站ID关注文章是" gnutella" ...我不认为有一个数字ID ...我从中提取id?

更新:

我从文章中提取帖子ID是1757 我已尝试使用此网址但未成功

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

另一种代码方法:

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

我尝试过这种方法......还没有!! 控制台成功返回但文本未正确发送... 也在设备上测试(排除可能的错误)......什么都没有!

2 个答案:

答案 0 :(得分:0)

您需要以key=value&key=value格式正确发送参数,而不仅仅是description的{​​{1}}。

NSDictionary

更新:

所以在查看发送的 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:@"&"]; 请求后(在this页面上:

POST

然而,这并没有大的帮助,因为您需要包含验证码

您要使用的是发布评论的WordPress API。我玩过这个,但我没有你的网站ID。

答案 1 :(得分:0)

试试这个:

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


}