我需要使用post方法將用戶名和密碼傳遞給url。我還想從service.can獲得響應,請告訴有效的方法來執行此操作嗎?將值傳遞給webservice使用POST方法
-3
A
回答
1
data not post through webservice json in ios
看看這個鏈接,通過我的問候發佈的問題一樣,你的問題的答案。希望它能幫助你。
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
相關問題
- 1. android將值傳遞給post方法
- 2. 將參數傳遞給POST方法
- 3. jquery ajax代碼沒有將值傳遞給webservice方法
- 4. 將.net webservice的值傳遞給jquery
- 5. 如何使用POST方法將javascript/jQuery變量傳遞給PHP
- 6. 無法將「+」傳遞給grails webservice URL
- 7. 無法將byte []數組傳遞給webservice
- 8. Alamofire POST方法不會將值傳遞給url
- 9. http post方法將空值傳遞給服務器
- 10. 如何使用Fiddler在post方法中將值傳遞給字符串數組?
- 11. 將值傳遞給委託方法
- 12. 將值傳遞給虛方法
- 13. 將值傳遞給方法onSucces
- 14. 將字符串值傳遞給方法
- 15. 將值傳遞給內部類方法?
- 16. 將值傳遞給另一個方法
- 17. 將值傳遞給方法的問題
- 18. AJAX:將值傳遞給ajax方法
- 19. POST參數傳遞給方法軌
- 20. 值傳遞給jQuery的POST
- 21. 將用戶憑據傳遞給Webservice
- 22. 將內部遞歸方法的值傳遞給外部方法
- 23. 將httpservletrequest傳遞給方法
- 24. 將ArrayList傳遞給方法?
- 25. 將CharArray傳遞給方法
- 26. 將int []傳遞給方法
- 27. 將Stringstream傳遞給方法
- 28. 將塊傳遞給方法
- 29. 如何從jQuery/ajax將參數傳遞給[OperationContract] webservice方法?
- 30. 將POST傳遞給兩頁?
你到目前爲止嘗試過什麼?一個簡單的形式應該做...沒有更多的信息(尤其是你已經嘗試過的信息),這是不可能的幫助 – codeling