c#HTTPWebRequest发布到Objective-c NSMutableURLRequest状态码405
C语言中美好而简单的东西在C语言中变成了熊c#HTTPWebRequest发布到Objective-c NSMutableURLRequest状态码405,objective-c,post,nsmutableurlrequest,Objective C,Post,Nsmutableurlrequest,C语言中美好而简单的东西在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 + "&passwor
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方法不允许”
任何关于在IOS上使用POST的帮助或提示都会非常有用 这些请求并不完全相同。 C#示例将POST请求发送到
/DebugUser
,查询参数为?用户名=&password=
,obj-C one将POST请求发送到//code>,格式为URLCoded data用户名=&password=
。我猜问题是URI路径中的这个小错误(大多数是那些小的、愚蠢的错误需要比实际问题更多的时间来解决……)。此外,我建议对参数进行url编码,在本例中,您的用户名me@inc.com
应编码为me%40inc.com
才能成为有效的url/表单url编码数据。另请参见我关于ivar的代码注释
类似的东西应该可以使用(即时编写,发布前我还没有编译/检查):
这些要求并不完全相同。
C#示例将POST请求发送到/DebugUser
,查询参数为?用户名=&password=
,obj-C one将POST请求发送到//code>,格式为URLCoded data用户名=&password=
。我猜问题是URI路径中的这个小错误(大多数是那些小的、愚蠢的错误需要比实际问题更多的时间来解决……)。此外,我建议对参数进行url编码,在本例中,您的用户名me@inc.com
应编码为me%40inc.com
才能成为有效的url/表单url编码数据。另请参见我关于ivar的代码注释
类似的东西应该可以使用(即时编写,发布前我还没有编译/检查):
C#中的用户名,objC中的用户名。在objC中使用小写的n可以吗?另外,您的objC代码不包含对DebugUser的引用,您在C#中的发布到/userName,在objC中的userName。在objC中使用小写的n可以吗?另外,您的objC代码不包含对DebugUser的引用,您发布到/Wow的帖子我一整天都在尝试至少10种不同的方法,但都犯了同样的小错误!我尝试的一些方法是巨大的。这很好,很简单,而且很有效。哇,我一整天都在用同样的小错误尝试至少10种不同的方法!我尝试的一些方法是巨大的。这是好的,简单的和工程。
-(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");
}
}
-(void)try10{
NSString *user = @"me%40inc.com";
NSString *pwd = @"myEazyPassword";
NSString *myURLString = [NSString stringWithFormat:@"http://192.168.1.10:8080/DebugUser?username=%@&password=%@",user,pwd];
NSMutableURLRequest *request = [NSMutableURLRequest new];
[request setURL:[NSURL URLWithString:myURLString]];
[request setHTTPMethod:@"POST"];
NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
if(theConnection){
// I suppose this one is ivar, its safer to use @property
// unless you want to implement some custom setters / getters
//webData = [NSMutableData data];
self.webData = [NSMutableData data];
NSLog(@"connection initiated");
}
}