简体   繁体   English

ASP.NET Web服务

[英]ASP.NET Webservice

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. 我对.net Web服务还很陌生,正在寻求一点帮助。

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 我创建了一个名为net.webservicex.www的Web代理,该代理指向以下位置的Web服务。http://www.webservicex.net/geoipservice.asmx

Here's my c# code. 这是我的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";

        }
    }
}

The error I'm recieving is Error 我收到的错误是错误

Cannot implicitly convert type 'web_services.net.webservicex.www.GeoIP' to 'string' at line 24 第24行无法将类型'web_services.net.webservicex.www.GeoIP'隐式转换为'string'

If someone could give me some tips or idea's that would be great. 如果有人可以给我一些提示或想法,那将很棒。

Thanks! 谢谢! Paul 保罗

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: 由于您的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;

Looking at the wsdl for this web service it seems that the call to the GetGeoIP method returns an object, not a string. 查看此Web服务的wsdl,似乎对GetGeoIP方法的调用返回一个对象,而不是字符串。

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. 您可以将结果类型更改为期望的对象,也可以使用var关键字。

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 . 该方法返回的对象不是字符串,而是web_services.net.webservicex.www.GeoIP

You've declared result as a string, which doesn't match. 您已将result声明为不匹配的字符串。 Perhaps there is a ToString() method on the GeoIP class. GeoIP类上可能有一个ToString()方法。 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. 根据result不同,您也许可以将其作为数据源。

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: 我是C#和.net的初学者,但是这是我解决此问题的方法,我相信它会对像我这样的初学者有所帮助:

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. 所以很明显(是的,对……吐血之后……大声笑),结果不可能是简单的STRING类型。 So, taking samples from the above solutions I've made it like that: 因此,从上述解决方案中抽取样本,我做到了:

Default.aspx.cs: 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. 其中-“ mygeoip ”是我的Web服务名称(而不是“ net.webservicex.www”),而GeoIPService是我的名称空间。

Default.aspx: 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>

That's it - I hope I've helped beginners like me :) 就是这样-我希望我对像我这样的初学者有所帮助:)

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM