谁可以手写C#代码,调用那个WebService接口,,麻烦写一下好吗,我知道我分不够,接口是这样
http://www.webxml.com.cn/WebServices/IpAddressSearchWebService.asmx在google里查过,,查不到,,才在这里发的,,
不要这种调用的1.在你的项目引用出,右键-》添加服务引用
2.填入接口地址,例如:http://www.webxml.com.cn/WebServices/IpAddressSearchWebService.asmx
3.确定后系统会自动生成一个代理类
4.直接在你的cs代码处调用代理类下的方法
http://www.webxml.com.cn/WebServices/IpAddressSearchWebService.asmx在google里查过,,查不到,,才在这里发的,,
不要这种调用的1.在你的项目引用出,右键-》添加服务引用
2.填入接口地址,例如:http://www.webxml.com.cn/WebServices/IpAddressSearchWebService.asmx
3.确定后系统会自动生成一个代理类
4.直接在你的cs代码处调用代理类下的方法
http://blog.chinaunix.net/u2/82078/showart_1292289.html
<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" /> <br />
</td>
</tr>
</table>
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("查询失败!");
}
}
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); //调用服务器返回开票数据
<%@ 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();
}
}
可以问一下这个是什么意思吗?为什么要把这个变成字节型的数组啊 byte[] buff
简单理解的话Stream.Write方法第一个参数需要的就是byte[]所以得转换。