2014-04-21 3 views
0

내 응용 프로그램에서 매개 변수 이름, 전자 메일, 웹 사이트 URL 및 블로그에 의견을 보내는 데이 코드를 사용하고 있습니다 ... 아마도 일부 값이 잘못 설정되어 있습니다 ... 누군가가 나를 도울 수 있습니까? 내가 미친거야!! 미리 감사드립니다!HTTP POST에 텍스트를 보내는 방법은 무엇입니까?

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


} 

는 편집 : 웹 서비스의 API 문서를 읽으려고했습니다

@ 부자, 더 정확하게 부분 "포스트에 코멘트를 만들기"우려, 내 사이트 ID의 우려를 생각 기사는 "gnutella"입니다 ... 나는 ID를 추출하는 숫자 ID가 있다고 생각하지 않습니다.

업데이트 :이 기사에서 포스트 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"]; 

또 다른 코드 방법 :

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

나는이 방법으로 시도했다. .. 아직!! 콘솔이 성공적으로 반환되지만 텍스트가 올바르게 전송되지 않습니다 ... 또한 장치에서 테스트되었습니다 (가능한 버그 제외) ... 아무 것도 없습니다!

답변

0

key=value&key=value의 형식으로 매개 변수를 올바르게 입력해야합니다. descriptionNSDictionary이 아닙니다.

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 :

그래서 this 페이지 (전송되는 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 

당신이 보안 문자를 포함 할 필요가로이 대규모하지만 도움이되지 않습니다

사용하려는 것은 게시 용으로 WordPress API입니다. 코멘트. 나는 이것으로 놀았지만 당신의 사이트 ID가 없습니다.

+0

내가 시도했지만 결과는 ... 다른 아이디어 게시되지 전에 ... 텍스트와 동일합니다? – geminimac

+0

@geminimac API 문서에서 요청 형식에 대해 무엇을 말합니까? – Rich

+0

어떤 API 문서를 언급하고 있습니까? 아마 내가 잘못된 객체 키를 ... – geminimac

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; 


} 
+0

시도했습니다 ... 작동하지 않습니다 ... – geminimac

관련 문제