我正在研究需要與Windows服務器共享數據的應用程序。我查看了網絡上的一些示例,並提出了下面列出的實現。雖然iOS代碼和Web服務獨立運行良好,但我不知道如何將它們連接在一起。我所見過的所有例子都沒有顯示網絡服務的URL(或者至少不能讓我弄清楚發生了什麼)。請看看我對ServerURL的價值,並建議它應該是什麼。我相信它應該在某處包含入口點「doSomething」的名稱,但我不知道語法應該是什麼樣子。我試過http://texas/WebSite3/Service.asmx/doSomething
和http://texas/WebSite3/Service.asmx?op=doSomething
,但似乎都沒有工作。請讓我知道是否有任何其他問題。如何從iOS設備發送JSON POST到ASP.NET URL
我認爲,一旦我得到它在我的本地系統上工作,我可以用完整的域名取代德克薩斯州。
這裏是iOS的代碼:
-(void)checkWithServer {
// Create the POST request payload.
NSDictionary *tmp = [[NSDictionary alloc]initWithObjectsAndKeys:
@"[email protected]", @"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];
}
下面是它談論的Web服務:
[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;
}
代碼正在工作,但它顯示一個不推薦使用的錯誤:(CompletionHandler已棄用使用NsUrlSession .. – 2016-04-25 05:19:28