2

我正在自学 Java,我对 C/C++ 和 C# 有丰富的经验。

我在 C# 中有一个 Web 服务,它具有将记录插入我的数据库的功能,只需一个用户名和密码。

    [WebMethod]
    public string InsertNewUser(string username, string password)
    {
        try
        {
            conn.Open();

            string insertQuery = "INSERT INTO Accounts(Username, Password) VALUES ('" + username + "','" + password + "')";

            SqlCommand cmd = new SqlCommand(insertQuery, conn);

            try
            {
                cmd.ExecuteNonQuery();
                conn.Close();
                return "New user added: " + username;
            }
            catch
            {
                return "Error in command execution";
            }
        }
        catch
        {
            return "Error in database connection";
        }
    }

谁能告诉我是否可以在 java 客户端应用程序中调用此函数?如果是这样,我该怎么做?我很难在网上找到很多关于 java 连接到 c# web 服务的信息。

目前我正在使用 asmx Web 服务,但考虑改用 WCF,这会使它更容易还是更难?

任何帮助是极大的赞赏。

谢谢

4

1 回答 1

0

A web service is a web service. That means you can call it from anywhere. Be it from C#, Java or anywhere else. The question is how you are exposing your web service. I recommend exposing it as a REST service, that way it will be accessible from everywhere. There is plenty of information available on the web on how to implement RESTful services. There is even a book, RESTFul .Net, that you can read. Once you implement the REST service, then you access it using HTTP GET, PUT, UPDATE, ETC... from your choice of environment.

于 2013-03-11T20:05:39.007 回答