【问题标题】:return more than one data with soap webservice [duplicate]使用soap webservice返回多个数据[重复]
【发布时间】:2013-06-14 12:19:14
【问题描述】:

我正在尝试创建一个将返回超过 1 个字符串的 Web 服务。它将返回 4 个字符串。我以前没有 webservices,我过去只返回 true 或 false 值。但现在我需要更多数据。

这是我的网络服务。

[WebMethod]
    public string get_currency(string date, string cur_code) {
        string rtn = "";
        try
        {
            using (SqlConnection conn = new SqlConnection("Data Source=xxx-xxxxx;Initial Catalog=xxx;Persist Security Info=True;User ID=xxx;Password=xxx"))
            {
                string selectSql = "select date,code,banknote_buying,banknote_selling from Currencies where date = '" + date + "' and code ='" + cur_code + "'";
                SqlCommand comm = new SqlCommand(selectSql, conn);
                conn.Open();
                SqlDataReader dr = comm.ExecuteReader();
                if (dr.Read()) {
                    rtn = dr["date"].ToString() +  dr["code"].ToString() +  dr["banknote_buying"].ToString() + dr["banknote_selling"].ToString();
                }
            }
        }
        catch (Exception ex)
        {
            return "Fail";
        }

        return rtn;
    }

我怎样才能将它们作为正确的 SOAP 对象返回

【问题讨论】:

  • 用特殊标记分隔字符串,返回字符串数组,构建具有 4 个字符串属性的复合类型等。很多选择。您尝试过什么?
  • 这是我在上面尝试过的,@asawyer。我问如何将它作为适当的肥皂对象发送
  • 任何可以在soap信封中序列化的东西都是“正确的soap对象” 你还有一个明显的sql注入攻击漏洞。

标签: c# asp.net web-services soap


【解决方案1】:

我终于做了我想做的事。这是我的代码。

    [WebMethod]
    public Currency_Object get_currency(string date, string cur_code) {

        Currency_Object co = new Currency_Object();
        try
        {
            using (SqlConnection conn = new SqlConnection("Data Source=xxx-xxx;Initial Catalog=MB_DB;Persist Security Info=True;User ID=xxx;Password=xxx"))
            {
                string selectSql = "select date,code,banknote_buying,banknote_selling from Currencies where date = '" + date + "' and code ='" + cur_code + "'";
                SqlCommand comm = new SqlCommand(selectSql, conn);
                conn.Open();
                SqlDataReader dr = comm.ExecuteReader();

                if (dr.Read()) {
                    co.date_ = dr["date"].ToString();
                    co.code_ = dr["code"].ToString();
                    co.banknote_buying_ = dr["banknote_buying"].ToString();
                    co.banknote_selling_ = dr["banknote_selling"].ToString();
                }
            }
        }
        catch (Exception ex) { return null; }
        return co;
    }
}

这就是返回的内容

 <Currency_Object xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://tempuri.org/">
  <date>06.06.2013 00:00:00</date>
  <code>USD</code>
  <banknote_buying>1,8847</banknote_buying>
  <banknote_selling>1,8922</banknote_selling>
 </Currency_Object>

【讨论】:

  • 我为您感到高兴,但请抽出时间解决您在此处遇到的 sql 注入问题。它可以像参数化命令文本一样简单。您还需要某种错误日志记录,或者让Currency_Object 能够将异常作为soap 消息的一部分报告给调用者。
【解决方案2】:

尝试返回字符串[]

[WebMethod]
public **string[]** get_currency(string date, string cur_code) {
    **List<string> rtn = new List<string> ();**
    try
    {
        using (SqlConnection conn = new SqlConnection("Data Source=xxx-xxxxx;Initial Catalog=xxx;Persist Security Info=True;User ID=xxx;Password=xxx"))
        {
            string selectSql = "select date,code,banknote_buying,banknote_selling from Currencies where date = '" + date + "' and code ='" + cur_code + "'";
            SqlCommand comm = new SqlCommand(selectSql, conn);
            conn.Open();
            SqlDataReader dr = comm.ExecuteReader();
            if (dr.Read()) {
                rtn.Add( dr["date"].ToString());
                rtn.Add(dr["code"].ToString());
                 rtn.Add(dr["banknote_buying"].ToString());
                 rtn.Add(dr["banknote_selling"].ToString());
            }
        }
    }
    catch (Exception ex)
    {
        rtn.Add("Fail");
    }

    return rtn.ToArray();
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-10-31
    • 1970-01-01
    • 1970-01-01
    • 2017-08-22
    • 2016-11-20
    • 1970-01-01
    • 1970-01-01
    • 2017-09-01
    相关资源
    最近更新 更多