【问题标题】:How I know the posting data goes correctly我如何知道发布数据正确
【发布时间】:2013-04-05 09:42:26
【问题描述】:
-(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";
    }

}

其中startConnection方法如下

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

【问题讨论】:

  • 检查服务器是否有相同的数据。
  • 检查服务器的响应。解析响应,您就知道发布数据是否正确
  • @user2248428 : 请详细解释
  • 当我在浏览器中输入本地主机字符串时,我会在浏览器中获取我想要在我的代码中的数据

标签: objective-c nsurlconnection nsurlrequest


【解决方案1】:

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

你需要实现一个委托:

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

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

编辑:

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

【讨论】:

猜你喜欢
  • 2020-10-04
  • 1970-01-01
  • 1970-01-01
  • 2019-07-16
  • 2021-04-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多