我如何知道发布数据正确无误 [英] How I know the posting data goes correctly

查看:38
本文介绍了我如何知道发布数据正确无误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

-(IBAction)clicked:(id)sender{NSString *CIDString = cID.text;NSURL *url = [NSURL URLWithString:@"http://localhost:8080/test/?"];NSString *postData = [NSString stringWithFormat:@"companyID=%@",CIDString];NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[url 标准化URL]cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:60];[请求 setHTTPMethod:@"POST"];[请求 setValue:@"application/x-www-form-urlencoded; charset=utf-8" forHTTPHeaderField:@"Content-Type"];[请求 setHTTPBody:[postData dataUsingEncoding:NSUTF8StringEncoding]];[self startConnection:(NSMutableURLRequest *)request];if([self.result isEqualToString:@"New Alert"]){cID.text = @"Scuess";}}

其中startConnection方法如下

- (void)startConnection:(NSMutableURLRequest *)request {[self.connection 取消];NSMutableData *data = [[NSMutableData alloc] init];self.receivedData = 数据;self.result = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];NSLog(@"receivedData: %@", [[NSString alloc] initWithData:receivedData encoding:NSASCIIStringEncoding]);self.connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];如果(self.networkErrorAlert){NSLog(@"连接失败");}[self.connection start];}

解决方案

当我在浏览器中输入 localhost 字符串时,我正在获取数据在我的代码中想要的浏览器

你需要实现一个委托:

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {NSLog(@"收到回复:%@", [回复网址]);//给你网址.[receivedData setLength:0];}

<块引用>

当服务器提供了足够的数据来创建一个NSURLResponse 对象,委托接收一个连接:didReceiveResponse:消息.委托方法可以检查提供的 NSURLResponse 并确定预期的内容数据的长度、MIME 类型、建议的文件名和其他元数据由服务器提供.

//如果你想要更多……你需要这样做:NSString *contentTypeValue = nil;for (NSString *headerKey in [[(NSHTTPURLResponse*)response allHeaderFields] allKeys] ) {if([@"content-type" caseInsensitiveCompare:headerKey] == NSOrderedSame ) {contentTypeValue = [[(NSHTTPURLResponse*)response allHeaderFields] valueForKey:headerKey];}}NSLog(@"====>%@",contentTypeValue);

-(IBAction)clicked:(id)sender{
    NSString *CIDString = cID.text;

    NSURL *url = [NSURL URLWithString:@"http://localhost:8080/test/?"];
    NSString *postData = [NSString stringWithFormat:@"companyID=%@",CIDString];

    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[url standardizedURL]
                                                           cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:60];

    [request setHTTPMethod:@"POST"];
    [request setValue:@"application/x-www-form-urlencoded; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
    [request setHTTPBody:[postData dataUsingEncoding:NSUTF8StringEncoding]];

    [self startConnection:(NSMutableURLRequest *)request];
     if([self.result isEqualToString:@"New Alert"])
    {
        cID.text = @"Scuess";
    }

}

where startConnection method is as follows

- (void)startConnection:(NSMutableURLRequest *)request {

    [self.connection cancel];

    NSMutableData *data = [[NSMutableData alloc] init];
    self.receivedData = data;
    self.result = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
    NSLog(@"receivedData: %@", [[NSString alloc] initWithData:receivedData encoding:NSASCIIStringEncoding]);


    self.connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];

    if (self.networkErrorAlert) {
        NSLog(@"connection fail");

    }

    [self.connection start];
}

解决方案

when i am typing the localhost string in the browser im getting data in the browser which i want in the my code

You need to implement a delegate:

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
    NSLog(@"Received response: %@", [response url]); //give you the url.        [receivedData setLength:0];
}

When the server has provided sufficient data to create an NSURLResponse object, the delegate receives a connection:didReceiveResponse: message. The delegate method can examine the provided NSURLResponse and determine the expected content length of the data, MIME type, suggested filename and other metadata provided by the server.

EDIT:

   //if you want something more...you need to do as :
    NSString *contentTypeValue = nil;
    for (NSString *headerKey in [[(NSHTTPURLResponse*)response allHeaderFields] allKeys] ) {
        if([@"content-type" caseInsensitiveCompare:headerKey] == NSOrderedSame ) {
            contentTypeValue = [[(NSHTTPURLResponse*)response allHeaderFields] valueForKey:headerKey];
        }
     }
     NSLog(@"===>%@",contentTypeValue);

这篇关于我如何知道发布数据正确无误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆