将数据从iPhone发送到服务器

时间:2015-04-28 23:05:07

标签: ios asp.net swift post

我试图将数据从iphone(iOS 8 - Swift)发送到服务器(asp.net)。 目前我可以从asp.net获取信息,但我无法从我的iPhone发布数据。

这是我在Swift中的函数 POST

let myUrl = NSURL(string: "http://192.168.0.79/JsonWcfService/GetEmployees.svc/json/InsertEmployee/");

    //let myUrl2 = NSURL(fileURLWithPath: str)
    let request = NSMutableURLRequest(URL:myUrl!);
    request.HTTPMethod = "POST";

    // Compose a query string
    let postString = "name=James&lastname=Bond";

    request.HTTPBody = postString.dataUsingEncoding(NSUTF8StringEncoding);

    let task = NSURLSession.sharedSession().dataTaskWithRequest(request) {
        data, response, error in

        if error != nil
        {
            println("error=\(error)")

            return
        }

        // You can print out response object
        println("response = \(response)")

        // Print out response body
        let responseString = NSString(data: data, encoding: NSUTF8StringEncoding)
        println("responseString = \(responseString)")

        //Let's convert response sent from a server side script to a NSDictionary object:

        var err: NSError?
        var myJSON = NSJSONSerialization.JSONObjectWithData(data, options: .MutableLeaves, error:&err) as? NSDictionary

        if let parseJSON = myJSON {
            // Now we can access value of First Name by its key
            var firstNameValue = parseJSON["firstName"] as? String
            println("firstNameValue: \(firstNameValue)")
        }

    }

    task.resume()

我在asp.net中的代码是:

   public interface IPostEmployees
{
    [OperationContract]
    [WebInvoke(Method = "POST",
        ResponseFormat = WebMessageFormat.Json,
        RequestFormat = WebMessageFormat.Json,
        BodyStyle = WebMessageBodyStyle.Wrapped,
        UriTemplate = "json/InsertEmployee/{id1}/{id2}")]
    bool InsertEmployeeMethod(string id1, string id2);
}

  public bool InsertEmployeeMethod(string id1, string id2)
   {

       int success = 0;
       using (SqlConnection conn = new SqlConnection("data source=SQLREPORTES;database=prueba;persist security info=True;user id=MYUSER;password=MYPASSWORD;multipleactiveresultsets=True;"))
       {
           conn.Open();

           //decimal value = Decimal.Parse(id3);
           string cmdStr = string.Format("INSERT INTO dbo.EmpDB VALUES('{0}','{1}')", id1, id2);
           SqlCommand cmd = new SqlCommand(cmdStr, conn);
           success = cmd.ExecuteNonQuery();

           conn.Close();
       }

       return (success != 0 ? true : false);
   }

我想发送一个NAME和一个LASTNAME,2个参数,但显示错误: 状态代码:404 END NOT FOUND

¿我做错了什么?感谢

PD:基于本教程:http://www.codeproject.com/Articles/405189/How-to-access-SQL-database-from-an-iPhone-app-Via

0 个答案:

没有答案