我正在开发一个需要与 Windows 服务器共享数据的应用程序。我查看了网络上的一些示例,并提出了下面列出的实现。虽然 iOS 代码和 Web 服务可以独立工作,但我不知道如何将它们连接在一起。我看到的所有示例都没有显示 Web 服务的 URL(或者至少没有以让我弄清楚发生了什么的方式)。请查看我对 ServerURL 的值并建议它应该是什么。我相信它应该在某处包含入口点“doSomething”的名称,但我不知道语法应该是什么样子。我已经尝试过http://texas/WebSite3/Service.asmx/doSomething
,http://texas/WebSite3/Service.asmx?op=doSomething
但似乎都不起作用。请让我知道是否存在任何其他问题。
我假设一旦我让它在我的本地系统上运行,我就可以用一个完整的域名替换 Texas。
这是iOS代码:
-(void)checkWithServer {
// Create the POST request payload.
NSDictionary *tmp = [[NSDictionary alloc]initWithObjectsAndKeys:
@"test@gmail.com", @"email",
@"John Doe", @"name",
nil];
NSError *error;
NSData *postdata = [NSJSONSerialization dataWithJSONObject:tmp options:0 error:&error];
NSString *serverURL = @"http://texas/WebSite3/Service.asmx";
// Create the POST request to the server.
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:serverURL]];
[request setHTTPMethod:@"POST"];
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setValue:[NSString stringWithFormat:@"%d", [postdata length]] forHTTPHeaderField:@"Content-Length"];
[request setHTTPBody:postdata];
NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];
[conn start];
}
这是它与之交谈的网络服务:
[WebService(Namespace = "http://texas/WebSite3")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
[System.Web.Script.Services.ScriptService]
public class Service : System.Web.Services.WebService
{
public Service () {
//Uncomment the following line if using designed components
//InitializeComponent();
}
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public string doSomething(string msg)
{
byte[] buffer = GetBytes(msg);
XmlReader reader = System.Runtime.Serialization.Json.JsonReaderWriterFactory.CreateJsonReader(buffer, System.Xml.XmlDictionaryReaderQuotas.Max);
XElement root = XElement.Load(reader);
string result = "{\"result\":\"none\"}";
return result;
}