ASP.NET Web服务

| 我可能会从错误的方向出发。我是.net Web服务的新手,正在寻求一点帮助。 我有一个在线定位的网络服务,我想将结果绑定到列表框或数据视图,但也无法。 我已经创建了一个名为net.webservicex.www的Web代理,该代理指向以下位置的Web服务。http://www.webservicex.net/geoipservice.asmx 这是我的C#代码。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace web_services
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            net.webservicex.www.GeoIPService myProxy = new net.webservicex.www.GeoIPService(); // proxy object
            string ipAddress, result;

            ipAddress = txtIpAddress.Text;

            result = myProxy.GetGeoIP(\"64.106.166.130\");
            lstResults.DataSource = result;
            lstResults.DataMember = \"IP\";

        }
    }
}
我收到的错误是错误 无法在第24行将类型\'web_services.net.webservicex.www.GeoIP \'隐式转换为\'string \' 如果有人可以给我一些提示或想法,那将很棒。 谢谢! 保罗     
已邀请:
您不需要将其放在结果字符串中
lstResults.DataSource = myProxy.GetGeoIP(\"64.106.166.130\");
由于您的Web服务返回的对象不可枚举,因此可以通过将其放入可枚举的类型来欺骗它:
List<web_services.net.webservicex.www.GeoIP> resultList = new List<web_services.net.webservicex.www.GeoIP>();
resultList.Add(myProxy.GetGeoIP(\"64.106.166.130\"));
lstResults.DataSource = resultList;
    
查看此Web服务的wsdl,似乎对GetGeoIP方法的调用返回一个对象,而不是字符串。 这就是编译器抱怨的原因。您可以将结果类型更改为期望的对象,也可以使用var关键字。     
这条线是错误的:
result = myProxy.GetGeoIP(\"64.106.166.130\");
该方法返回的对象不是字符串,而是4。 您已将“ 5”声明为不匹配的字符串。
GeoIP
类上可能有
ToString()
方法。如果是这样,您可以将代码更改为:
string ipAddress;
web_services.net.webservicex.www.GeoIP result;
// or maybe: object result;

ipAddress = txtIpAddress.Text;

result = myProxy.GetGeoIP(\"64.106.166.130\");
根据“ 5”的样子,您也许可以将其作为数据源。     
我是C#和.net的初学者,但是这是我解决此问题的方法,我相信它将对像我这样的初学者有所帮助: 结果对象如下所示:
<GeoIP><ReturnCode>1</ReturnCode><IP>11.22.33.44</IP><ReturnCodeDetails>Success</ReturnCodeDetails><CountryName>Germany</CountryName><CountryCode>GER</CountryCode></GeoIP>
所以很明显(是的,对……吐血后……大声笑),结果不可能是简单的STRING类型。 因此,从上述解决方案中抽取样本,我做到了: Default.aspx.cs:
        mygeoip.GeoIPService getIP = new mygeoip.GeoIPService();
        string myIP = IPTextBox.Text;
        GeoIPService.mygeoip.GeoIP resultList = new GeoIPService.mygeoip.GeoIP();

        resultList = getIP.GetGeoIP(myIP);
        sCountry.Text = resultList.CountryName;
        sCountryCode.Text = resultList.CountryCode;
        sIP.Text = resultList.IP;            
其中-\“ mygeoip \”是我的Web服务名称(而不是\“ net.webservicex.www \”) GeoIPService是我的命名空间。 Default.aspx:
        <asp:TextBox ID=\"IPTextBox\" runat=\"server\"></asp:TextBox>&nbsp;<asp:Button 
        ID=\"GetWhois\" runat=\"server\" Text=\"Get Whois\" onclick=\"GetWhois_Click\" />
<p><asp:Label ID=\"sCountry\" runat=\"server\" Text=\"Country: \"></asp:Label></p>
<p><asp:Label ID=\"sCountryCode\" runat=\"server\" Text=\"Country: \"></asp:Label></p>
<p><asp:Label ID=\"sIP\" runat=\"server\" Text=\"Country: \"></asp:Label></p>
就是这样-我希望我已经对像我这样的初学者有所帮助:)     
    GeoIP result;

    ipAddress = \"196.36.153.129\";

    result = myProxy.GetGeoIP(\"64.106.166.130\");
    
[WebMethod]
        public double ProcitajKursNaDan(DateTime datum, string valuta) {
            List<string> podaci = GetLines(\"valute.txt\");

            double kurs = 0.0;

      
            for (int i = 0; i < podaci.Count; i++) {
                string[] linija = podaci[i].Split(\'|\');
              
                string dat = linija[0];
                string val = linija[1];
                string vrednost = linija[2];

         
                dat = dat.Trim(); 
                val = val.Trim(); 
                vrednost = vrednost.Trim();

               
                DateTime datIzFajla = DateTime.ParseExact(dat, \"d/M/yyyy\", null);

                double kursIzFajla = Convert.ToDouble(vrednost);


                if (DateTime.Compare(datIzFajla, datum) == 0 && val == valuta)
                    kurs = kursIzFajla;
            }


            return kurs;
        }

        [WebMethod]
        public bool UpisiKursNaDan(DateTime datum, string valuta, double Kurs) {
            string date = datum.ToString(\"d/M/yyyy\");
            string linijaZaUpis = date + \" | \" + valuta + \" | \" + Kurs.ToString();

            bool success = false;

            try
            {
                StreamWriter sw = new StreamWriter(Server.MapPath(\"podaci/valute.txt\"), true);
                sw.WriteLine(linijaZaUpis);
                sw.Close();

                success = true;
            }
            catch {
                success = false;
            }
            return success;
        }

        [WebMethod]
        public List<string> ProcitajSveValute() {
            List<string> linije = GetLines(\"valute.txt\");

            List<string> ValuteIzFajla = new List<string>();

            for (int i = 0; i < linije.Count; i++) {
                string linija = linije[i];
                string valuta = linija.Split(\'|\')[1];
                valuta = valuta.Trim();
                ValuteIzFajla.Add(valuta);
            }

            List<string> ValuteKraj = ValuteIzFajla.Distinct().ToList();
            return ValuteKraj;
             //        try
    //        {
    //            if (!IsPostBack)
    //            {
    //                Service1 servis = new Service1();
    //                List<string> lista = servis.ProcitajSveValute().ToList<string>();
    //                for (int i = 0; i < lista.Count(); i++)
    //                {
    //                    DropDownList1.Items.Add(lista[i]);
    //                }

    //            }
    //        }
    //        catch (Exception ex)
    //        {
    //            Response.Write(ex.Message);
    //        }
    //    }

    //    protected void Button1_Click(object sender, EventArgs e)
    //    {
    //        try
    //        {
    //            Service1 servis = new Service1();
    //            DateTime datum = Calendar1.SelectedDate;
    //            string valuta = DropDownList1.SelectedItem.ToString();
    //            double kurs = Convert.ToDouble(TextBox1.Text);
    //            bool nesto = servis.UpisiKursNaDan(datum, valuta, kurs);
    //        }
    //        catch (Exception ex)
    //        {
    //            Response.Write(ex.Message);
    //        }
    //        TextBox1.Text = \" \";
        }
    }
}
        //    try
        //    {
        //        if (!IsPostBack)
        //        {
        //            Service1 servis = new Service1();
        //            List<string> lista = servis.ProcitajSveValute().ToList<string>();
        //            for (int i = 0; i < lista.Count(); i++)
        //            {
        //                DropDownList1.Items.Add(lista[i]);
        //            }

        //        }
        //    }
        //    catch (Exception ex)
        //    {
        //        Response.Write(ex.Message);
        //    }
        //}

        //protected void Button1_Click(object sender, EventArgs e)
        //{
        //    try
        //    {
        //        Service1 servis = new Service1();
        //        DateTime datum = Calendar1.SelectedDate;
        //        string valuta = DropDownList1.SelectedItem.ToString();
        //        double kurs = servis.ProcitajKursNaDan(datum, valuta);
        //        Label1.Text = kurs.ToString();
        //        if (kurs == 0)
        //        {
        //            Label1.Text = \"Ne postoji kursna lista za tu valutu na taj datum!\";
        //        }
        //    }
        //    catch (Exception ex)
        //    {
        //        Response.Write(ex.Message);
        //    }
        //}

要回复问题请先登录注册