ASP.NET 网络服务

发布于 2024-11-01 18:23:23 字数 1204 浏览 1 评论 0原文

我可能是从错误的方向来处理这个问题的。我对 .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";

        }
    }
}

我收到的错误是错误

无法隐式地将类型'web_services.net.webservicex.www.GeoIP'转换为第24行的'字符串'

如果有人能给我一些提示或想法那就太好了。

谢谢! 保罗

I may be going at this from the wrong direction. I'm fairly new to .net web services and was looking for a little help.

I have a geolocation webservice I got online and I wanted to bind the results to a listbox or a dataview but am unable too.

I've created a web proxy called net.webservicex.www that points to the webservice at.. http://www.webservicex.net/geoipservice.asmx

Here's my c# code.

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";

        }
    }
}

The error I'm recieving is Error

Cannot implicitly convert type 'web_services.net.webservicex.www.GeoIP' to 'string' at line 24

If someone could give me some tips or idea's that would be great.

Thanks!
Paul

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

甜`诱少女 2024-11-08 18:23:23

<罢工>
您不需要将其放入结果字符串

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;


You don't need to put it in the result string

lstResults.DataSource = myProxy.GetGeoIP("64.106.166.130");

Since the object returned by your webservice is not enumerable, You could trick it by putting it into an a enumerable Type:

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;
Oo萌小芽oO 2024-11-08 18:23:23

查看此 Web 服务的 wsdl,似乎对 GetGeoIP 方法的调用返回一个对象,而不是字符串。

这就是编译器抱怨的原因。您可以将结果类型更改为其期望的对象,也可以使用 var 关键字。

Looking at the wsdl for this web service it seems that the call to the GetGeoIP method returns an object, not a string.

This is why the compiler is complaining. You could either change the type of result to the object it is expecting or use the var keyword.

终止放荡 2024-11-08 18:23:23

此行有错误:

result = myProxy.GetGeoIP("64.106.166.130");

该方法返回的对象不是字符串,而是 web_services.net.webservicex.www.GeoIP

您已将 result 声明为字符串,但该字符串不匹配。也许 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");

并且根据结果的外观,您也许可以将其作为您的数据源。

This line is at fault:

result = myProxy.GetGeoIP("64.106.166.130");

The object returned by that method is not a string, it's a web_services.net.webservicex.www.GeoIP.

You've declared result as a string, which doesn't match. Perhaps there is a ToString() method on the GeoIP class. If so, you could change your code to:

string ipAddress;
web_services.net.webservicex.www.GeoIP result;
// or maybe: object result;

ipAddress = txtIpAddress.Text;

result = myProxy.GetGeoIP("64.106.166.130");

And depending on what result looks like, you might just be able to make it your data source.

倚栏听风 2024-11-08 18:23:23

我是 C# 和 .net 的初学者,但这是我解决这个问题的方法,我相信它会帮助像我这样的初学者:

结果对象看起来像这样:

<GeoIP><ReturnCode>1</ReturnCode><IP>11.22.33.44</IP><ReturnCodeDetails>Success</ReturnCodeDetails><CountryName>Germany</CountryName><CountryCode>GER</CountryCode></GeoIP>

很明显(是的,对......在吐血之后this...lol),结果不能是简单的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” 是我的 WebService 名称(而不是“net”) .webservicex.www")
GeoIPService 是我的命名空间。

Default.aspx:

        <asp:TextBox ID="IPTextBox" runat="server"></asp:TextBox> <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>

就是这样 - 我希望我能帮助像我这样的初学者:)

I'm a very beginner with the C# and .net but here's how I solved this and I'm sure it'll help beginners like me:

The result object looks like this:

<GeoIP><ReturnCode>1</ReturnCode><IP>11.22.33.44</IP><ReturnCodeDetails>Success</ReturnCodeDetails><CountryName>Germany</CountryName><CountryCode>GER</CountryCode></GeoIP>

So obviously (yeah right...after spitting blood on this...lol), the result cannot be a simple STRING type.
So, taking samples from the above solutions I've made it like that:

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;            

Where - "mygeoip" is my WebService name (instead of "net.webservicex.www")
and GeoIPService is my namespace.

Default.aspx:

        <asp:TextBox ID="IPTextBox" runat="server"></asp:TextBox> <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>

That's it - I hope I've helped beginners like me :)

路还长,别太狂 2024-11-08 18:23:23
    GeoIP result;

    ipAddress = "196.36.153.129";

    result = myProxy.GetGeoIP("64.106.166.130");
    GeoIP result;

    ipAddress = "196.36.153.129";

    result = myProxy.GetGeoIP("64.106.166.130");
乄_柒ぐ汐 2024-11-08 18:23:23
[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);
        //    }
        //}

[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);
        //    }
        //}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文