2013-11-22 70 views
-3

我需要使用post方法將用戶名和密碼傳遞給url。我還想從service.can獲得響應,請告訴有效的方法來執行此操作嗎?將值傳遞給webservice使用POST方法

+0

你到目前爲止嘗試過什麼?一個簡單的形式應該做...沒有更多的信息(尤其是你已經嘗試過的信息),這是不可能的幫助 – codeling

回答

1

添加此代碼上的.h文件

@interface xyz 
    @property (retain, nonatomic) NSMutableData *receivedData; 
    @property (retain, nonatomic) NSURLConnection *connection; 

加上.m文件 @implementation這個代碼........

@synthesize receivedData,connection; 
-(void)requesttoserver 
{ 

    //if there is a connection going on just cancel it. 
    [self.connection cancel]; 

    //initialize new mutable data 
    NSMutableData *data = [[NSMutableData alloc] init]; 
    self.receivedData = data; 


    //initialize url that is going to be fetched. 
    NSURL *url = [NSURL URLWithString:@"https://www.your urlname"]; 

    //initialize a request from url 
    request = [NSMutableURLRequest requestWithURL:[url standardizedURL]]; 

    //set http method 
    [request setHTTPMethod:@"POST"]; 

    //initialize a post data 

    NSString *postData = @"username password"; 


    //set request content type we MUST set this value. 


    [request setValue:@"application/x-www-form-urlencoded;" forHTTPHeaderField:@"Content-Type" ]; 

    //set post data of request 
    [request setHTTPBody:[postData dataUsingEncoding:NSUTF8StringEncoding]]; 


    //initialize a connection from request 
    connection = [[NSURLConnection alloc] initWithRequest:request delegate:self]; 

    self.connection = connection; 
    //[connection release]; 

    //start the connection 
    [connection start]; 


} 


-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{ 
    [self.receivedData appendData:data]; 

} 
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{ 

    NSLog(@"error%@" , error); 
} 
-(void)connectionDidFinishLoading:(NSURLConnection *)connection{ 
    NSString *htmlSTR = [[NSString alloc] initWithData:self.receivedData encoding:NSUTF8StringEncoding]; 





} 

調用requesttoserver方法,當你想發佈數據

+0

@Shan試試這個,並告訴我結果 – Sport

1

我試過這個

NSString *Post=[NSString stringWithFormat:@"email=%@&password=%@&password_confirmation=%@",[textfieldArray objectAtIndex:0],[textfieldArray objectAtIndex:1],[textfieldArray objectAtIndex:2]]; 

NSData *PostData = [Post dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:NO]; 
NSString *PostLengh=[NSString stringWithFormat:@"%d",[Post length]]; 
NSURL *Url=[NSURL URLWithString: @"http://offers2win.com/api/v1/users/"]; 

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:Url  cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0]; 
[request setHTTPMethod:@"POST"]; 
[request setValue:PostLengh forHTTPHeaderField:@"Content-Lenght"]; 
[request setHTTPBody:PostData]; 

NSData *ReturnData =[NSURLConnection sendSynchronousRequest:request returningResponse:Nil error:Nil]; 

NSString *Response = [[NSString alloc] initWithData:ReturnData encoding:NSUTF8StringEncoding]; 

//Response = [Response stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]; 

NSLog(@"Response%@",Response); 
+2

如果這是嘗試代碼請添加它在你的問題,而不是回答。如果此代碼是解決方案,請將其標記爲已接受的答案。 – Amar