2013-04-05 28 views
0

您好我是新与iPhone开发一些可以解释一个我 如何发布数据以URL并获得成功的响应形式,它 下面是示例代码...后的数据,并获得成功的响应

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

能有人帮我...

+1

这是什么,你的[前一个问题]之间的differnce(http://stackoverflow.com/questions/15830645/how-i-know-the-the-posting-data- GOES-正确) – 2013-04-05 10:09:01

回答

0

- (无效){sendRequest将

NSString *tempusername = txtusername.text; 
NSString *tmppassword = txtpassword.text; 
NSString *tempurllogin = [staticstrings URLLink]; 
NSString *tmpURL = [NSString stringWithFormat:@"http://example.com/ValidateService?", tempurllogin]; 
NSString *connect; 
connect = [NSString stringWithFormat:@"%@emailAddress=%@&password=%@", tmpURL,tempusername,tmppassword]; 

responseData = [[NSMutableData data] retain]; 
NSURL *url = [[NSURL alloc] initWithString:connect]; 
NSURLRequest *request = [NSURLRequest requestWithURL:url]; 
[[NSURLConnection alloc] initWithRequest:request delegate:self]; 

[url release]; 
[request release]; 

}