谁可以手写C#代码,调用那个WebService接口,,麻烦写一下好吗,我知道我分不够,接口是这样
http://www.webxml.com.cn/WebServices/IpAddressSearchWebService.asmx在google里查过,,查不到,,才在这里发的,,
不要这种调用的1.在你的项目引用出,右键-》添加服务引用
2.填入接口地址,例如:http://www.webxml.com.cn/WebServices/IpAddressSearchWebService.asmx
3.确定后系统会自动生成一个代理类
4.直接在你的cs代码处调用代理类下的方法

解决方案 »

  1.   

    你不想通过vs平台手动添加,也可以,就是利用编译器命令,比如c#的csc编译器来引入webservice,具体你可以参考:
    http://blog.chinaunix.net/u2/82078/showart_1292289.html
      

  2.   

      前台代码:
    <span>IP地址查询</span>
                        <table>
                            <tr>
                                <td>
                                    <asp:Label ID="Label11" runat="server" Text="请输入IP地址"></asp:Label>
                                </td>
                                <td>
                                    <asp:TextBox ID="txtip" runat="server"></asp:TextBox>
                                </td>
                            </tr>
                            <tr>
                                <td>
                                    <asp:Label ID="Label12" runat="server" Text="IP归属地:"></asp:Label>
                                </td>
                                <td>
                                    <asp:TextBox ID="IPAdress" runat="server" Enabled="false"></asp:TextBox>
                                </td>
                            </tr>
                            <tr>
                                <td align="right">
                                </td>
                                <td>
                                    <asp:Button ID="btnIP" runat="server" Text="查询IP" OnClick="btnIP_Click" />&nbsp;<br />
                                </td>
                            </tr>
                        </table>
      

  3.   

    添加web引用
    http://www.webxml.com.cn/WebServices/IpAddressSearchWebService.asmx
    Web引用名 :IpAdress
    添加命名空间 using IpAdress;
    实例化:
        IpAdress.IpAddressSearchWebService ip = new IpAddressSearchWebService();
    后台代码:  protected void btnIP_Click(object sender, EventArgs e)
        {
            try
            {
                string[] strip;
                string strtxeip = this.txtip.Text.Trim();
                if (strtxeip != null && strtxeip != "")
                {
                    strip = ip.getCountryCityByIp(strtxeip);
                }
                else
                {
                    strip = ip.getGeoIPContext();
                    txtip.Text = strip[0];
                }
                IPAdress.Text = strip[1];
            }
            catch (Exception ex)
            {
                Response.Write("查询失败!");
            }
        }
      

  4.   

    方法二:完全动态处理,传入服务服务网址,方法名和参数即可.using System; 
    using System.Net; 
    using System.IO; 
    using System.CodeDom; 
    using Microsoft.CSharp; 
    using System.CodeDom.Compiler; 
    using System.Web.Services.Description; 
    using System.Web.Services.Protocols; namespace HB.Common 

        /* 调用方式 
         *   string url = "http://www.webservicex.net/globalweather.asmx" ; 
         *   string[] args = new string[2] ; 
         *   args[0] = "Hangzhou"; 
         *   args[1] = "China" ; 
         *   object result = WebServiceHelper.InvokeWebService(url ,"GetWeather" ,args) ; 
         *   Response.Write(result.ToString()); 
         */ 
        public class WebServiceHelper 
        { 
            #region InvokeWebService 
            /// <summary> 
            /// 动态调用web服务 
            /// </summary> 
            /// <param name="url">WSDL服务地址</param> 
            /// <param name="methodname">方法名</param> 
            /// <param name="args">参数</param> 
            /// <returns></returns> 
            public static object InvokeWebService(string url, string methodname, object[] args) 
            { 
                return WebServiceHelper.InvokeWebService(url, null, methodname, args); 
            }         /// <summary> 
            /// 动态调用web服务 
            /// </summary> 
            /// <param name="url">WSDL服务地址</param> 
            /// <param name="classname">类名</param> 
            /// <param name="methodname">方法名</param> 
            /// <param name="args">参数</param> 
            /// <returns></returns> 
            public static object InvokeWebService(string url, string classname, string methodname, object[] args) 
            { 
                string @namespace = "EnterpriseServerBase.WebService.DynamicWebCalling"; 
                if ((classname == null) || (classname == "")) 
                { 
                    classname = WebServiceHelper.GetWsClassName(url); 
                }             try 
                { 
                    //获取WSDL 
                    WebClient wc = new WebClient(); 
                    Stream stream = wc.OpenRead(url + "?WSDL"); 
                    ServiceDescription sd = ServiceDescription.Read(stream); 
                    ServiceDescriptionImporter sdi = new ServiceDescriptionImporter(); 
                    sdi.AddServiceDescription(sd, "", ""); 
                    CodeNamespace cn = new CodeNamespace(@namespace);                 //生成客户端代理类代码 
                    CodeCompileUnit ccu = new CodeCompileUnit(); 
                    ccu.Namespaces.Add(cn); 
                    sdi.Import(cn, ccu); 
                    CSharpCodeProvider icc = new CSharpCodeProvider();                 //设定编译参数 
                    CompilerParameters cplist = new CompilerParameters(); 
                    cplist.GenerateExecutable = false; 
                    cplist.GenerateInMemory = true; 
                    cplist.ReferencedAssemblies.Add("System.dll"); 
                    cplist.ReferencedAssemblies.Add("System.XML.dll"); 
                    cplist.ReferencedAssemblies.Add("System.Web.Services.dll"); 
                    cplist.ReferencedAssemblies.Add("System.Data.dll");                 //编译代理类 
                    CompilerResults cr = icc.CompileAssemblyFromDom(cplist, ccu); 
                    if (true == cr.Errors.HasErrors) 
                    { 
                        System.Text.StringBuilder sb = new System.Text.StringBuilder(); 
                        foreach (System.CodeDom.Compiler.CompilerError ce in cr.Errors) 
                        { 
                            sb.Append(ce.ToString()); 
                            sb.Append(System.Environment.NewLine); 
                        } 
                        throw new Exception(sb.ToString()); 
                    }                 //生成代理实例,并调用方法 
                    System.Reflection.Assembly assembly = cr.CompiledAssembly; 
                    Type t = assembly.GetType(@namespace + "." + classname, true, true); 
                    object obj = Activator.CreateInstance(t); 
                    System.Reflection.MethodInfo mi = t.GetMethod(methodname);                 return mi.Invoke(obj, args);                 /* 
                    PropertyInfo propertyInfo = type.GetProperty(propertyname); 
                    return propertyInfo.GetValue(obj, null); 
                    */ 
                } 
                catch (Exception ex) 
                { 
                    throw new Exception(ex.InnerException.Message, new Exception(ex.InnerException.StackTrace)); 
                } 
            }         private static string GetWsClassName(string wsUrl) 
            { 
                string[] parts = wsUrl.Split('/'); 
                string[] pps = parts[parts.Length - 1].Split('.');             return pps[0]; 
            } 
            #endregion 
        } 
    }
    返回时如果不是字符串,即强制转换,如返回是DataSet,则string url = "http://www.webservicex.net/globalweather.asmx" ; 
    string[] args = new string[2] ; 
    args[0] = "Hangzhou"; 
    args[1] = "China" ; 
    object result = WebServiceHelper.InvokeWebService(url ,"GetWeather" ,args) ; 
    DataSet DSRe=(DataSet)result; 方法三:URL Behavior 属性 
      
    如果知道服务的方法和参数,只是调用的URL网址会随时变化,那么可以手工创建一个服务,添加上对应的的方法和传入参数,然后引入到项目中,就可以直接开发,在创建服务的实例化时,才修改对应的URL即可.例如服务中有个方法叫GetTax,那么就可以这样改:GetTax.GetTax GetTax1 = new GetTax.GetTax(); 
    GetTax1.Url = "http://" + WebIp1 + "/pub_wa_gspsp1/gettax.asmx";        //动态引入服务器 
                     
    DataSet DS1 = GetTax1.GetTaxMx(Bm1, OldBz, Fpl, SLx, StaDa, EndDa);   //调用服务器返回开票数据
      

  5.   


    <%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server">
        <title>无标题页</title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
            <asp:Label ID="Label1" runat="server" Text="要查询的IP"></asp:Label><br />
            <asp:TextBox ID="txtIP" runat="server">127.0.0.1</asp:TextBox>
            <asp:Button ID="btnSubmit" runat="server" OnClick="btnSubmit_Click" Text="查询" /><br />
            <asp:Label ID="sdfsdf" runat="server" Text="结果"></asp:Label><br />
            <asp:TextBox ID="txtResult" runat="server" Height="20px" Width="146px"></asp:TextBox>
        </div>
        </form>
    </body>
    </html>using System;
    using System.Data;
    using System.Configuration;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;
    using System.Net;
    using System.IO;
    using System.Xml;public partial class _Default : System.Web.UI.Page 
    {
        protected void Page_Load(object sender, EventArgs e)
        {    }
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            string strMatch = @"^\d+\.\d+\.\d+\.\d+$";
            if (!System.Text.RegularExpressions.Regex.IsMatch(txtIP.Text, strMatch))
            {
                txtIP.Text = "IP地址输入有误!";
                return;
            }        string strReqXML = "<?xml version=\"1.0\" encoding=\"utf-8\"?><soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\"><soap:Body><getCountryCityByIp xmlns=\"http://WebXml.com.cn/\"><theIpAddress>"+txtIP.Text+"</theIpAddress></getCountryCityByIp></soap:Body></soap:Envelope>";
            byte[] buff = System.Text.Encoding.UTF8.GetBytes(strReqXML);
            HttpWebRequest hwr = (HttpWebRequest)HttpWebRequest.Create("http://www.webxml.com.cn/WebServices/IpAddressSearchWebService.asmx");
            hwr.ContentType = "text/xml; charset=utf-8";
            hwr.ContentLength = buff.Length;
            hwr.Method = "POST";
            Stream sr = hwr.GetRequestStream();
            sr.Write(buff, 0, buff.Length);
            WebResponse wr = hwr.GetResponse();
            DataSet ds = new DataSet();
            ds.ReadXml(wr.GetResponseStream());
            txtResult.Text = ds.Tables[3].Rows[1][0].ToString();
            wr.Close();
            sr.Close();
        }
    }
      

  6.   

    evionmzs谢谢你啦byte[] buff = System.Text.Encoding.UTF8.GetBytes(strReqXML);
    可以问一下这个是什么意思吗?为什么要把这个变成字节型的数组啊  byte[] buff
      

  7.   

    这个该怎么说呢,网络传输的实际上就是bit流,也就是byte数组。
    简单理解的话Stream.Write方法第一个参数需要的就是byte[]所以得转换。
      

  8.   

    我调用 webserver 总是提示这个错误,请高人指点下 Email:[email protected]未能加载文件或程序集“11264 bytes loaded from System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089”或它的某一个依赖项。试图加载格式不正确的程序。