【问题标题】:Calling C# webMethod in Objective C在 Objective C 中调用 C# webMethod
【发布时间】:2012-11-19 13:22:04
【问题描述】:

我正在尝试与这个简单的 C# webMethod 进行通信:

[WebMethod()]
public static string login(string userName)
{
     if (userName == "test"){
         return "validated";
     }
     return "error";
}

我正在尝试使用以下 Objective C 代码连接到它:

NSString *userName = @"test";
NSString *post = [NSString stringWithFormat:@"userName=%@", userName];
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];

NSString *serverURLString = @"http://myURL.aspx/login";
NSURL *serverURL = [NSURL URLWithString:serverURLString];

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:serverURL];
[request setHTTPMethod:@"POST"];
[request addValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setHTTPBody:postData];

NSHTTPURLResponse *urlResponse = nil;
NSError *error = nil;
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&urlResponse error:&error];

NSString *result = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
NSLog(@"result = %@", result);

我得到的不是打印出“已验证”或“错误”,而是:

result = <!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head><title>
</title></head>
<body>
<form method="post" action="login" id="form1">
<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE"   value="/wEPDwULLTE2MTY2ODcyMjlkZFwRG2UySOwu+02xaz+VP6mmD60UBRkXvHCHxGohOt9d" />
<div>    
</div>
</form>
</body>
</html>

谁能告诉我 Web 方法或 Objective C 代码是否有明显问题?谢谢。

【问题讨论】:

    标签: c# objective-c http-post httpresponse webmethod


    【解决方案1】:

    我相信WebMethods 是SOAP,所以与其手动进行,为什么不使用API​​ 来处理SOAP?如wsdl2objc

    【讨论】:

    • 谢谢,我刚接触 Obj-C,从来没有听说过这个 API,但如果它使整个过程更简单,它会为我做的。我去看看。
    • 我最终得到了它的工作,虽然我不完全确定如何。 IK 认为服务器最初的设置方式存在错误。尽管如此,我会研究 wsdl2objc 以查看它是否对其余过程有帮助,所以我会接受这个答案。
    猜你喜欢
    • 2013-10-07
    • 1970-01-01
    • 1970-01-01
    • 2023-03-29
    • 2016-06-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多