使用 POST 方法将值传递给 Web 服务 [关闭]

Posted

技术标签:

【中文标题】使用 POST 方法将值传递给 Web 服务 [关闭]【英文标题】:Pass value to webservice usng POST method [closed] 【发布时间】:2013-11-22 07:13:20 【问题描述】:

我需要使用 post 方法将用户名和密码传递给 url。我还想从服务中获取响应。谁能告诉我有效的方法吗?

【问题讨论】:

到目前为止您尝试过什么?一个简单的表格应该可以...如果没有更多信息(尤其是您已经尝试过的信息),就不可能提供帮助 【参考方案1】:

data not post through webservice json in ios

查看此链接以获取我发布的与您的问题相同的问题的答案。希望对你有帮助。

【讨论】:

【参考方案2】:

将此代码添加到 .h 文件中

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

将此代码添加到 .m 文件中 @实现........

@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方法

【讨论】:

@Shan 试试这个并告诉我结果【参考方案3】:

我试过这个

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

【讨论】:

如果这是尝试代码,请将其添加到您的问题中,而不是作为答案。如果此代码是解决方案,请将其标记为已接受的答案。

以上是关于使用 POST 方法将值传递给 Web 服务 [关闭]的主要内容,如果未能解决你的问题,请参考以下文章

邮递员插件将值[FromBody]传递给发布方法时的空值[重复]

如何将值传递给正在加载反应本机应用程序的 Web 视图

从窗口将值传递给 iframe

将值传递给注入的EJB

Gatling 2.0.3 - 将值映射到模板

使用Post方法为Rest Web服务设置setRequestBody