C# 中美好而简单的东西在 Objective C 中变成了熊
static private void AddUser(string Username, string Password)
{
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(new Uri("http://192.168.1.10:8080/DebugUser?userName=" + Username + "&password=" + Password));
request.Method = "POST";
request.ContentLength = 0;
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Console.Write(response.StatusCode);
Console.ReadLine();
}
工作正常,但是当我尝试将其转换为 Objective-C (IOS) 时,我得到的只是“不允许连接状态 405 方法”
-(void)try10{
NSLog(@"Web request started");
NSString *user = @"me@inc.com";
NSString *pwd = @"myEazyPassword";
NSString *post = [NSString stringWithFormat:@"username=%@&password=%@",user,pwd];
NSData *postData = [post dataUsingEncoding:NSUTF8StringEncoding];
NSString *postLength = [NSString stringWithFormat:@"%ld", (unsigned long)[postData length]];
NSLog(@"Post Data: %@", post);
NSMutableURLRequest *request = [NSMutableURLRequest new];
[request setURL:[NSURL URLWithString:@"http://192.168.1.10:8080"]];
[request setHTTPMethod:@"POST"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];
NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
if(theConnection){
webData = [NSMutableData data];
NSLog(@"connection initiated");
}
}
任何有关在 IOS 上使用 POST 的帮助或指示都会有很大帮助。