我怎么知道发布数据变为正确(How I know the the posting data goes

2019-08-19 18:40发布

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

Answer 1:

当我在浏览器中键入IM本地主机字符串中的浏览器中获取数据,我想在我的代码

您需要实现一个委托:

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


文章来源: How I know the the posting data goes correctly