一、发送Http请求调用webService
如果调用WebService的不是在.NET中,无法直接添加web引用,那怎么调用webservice。
有两种方式
第一种方式:GET方式
![](http://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
1 string strUrl = "http://127.0.0.1/rss/webservice.asmx/GetNews?topNum=2"; 2 HttpWebRequest hwrq = (HttpWebRequest)WebRequest.Create(strUrl); 3 hwrq.Method = "GET"; 4 HttpWebResponse hwrp = (HttpWebResponse)hwrq.GetResponse(); 5 Stream stream = hwrp.GetResponseStream(); 6 XmlDocument doc = new XmlDocument(); 7 doc.Load(stream); 8 string strLine = ""; 9 XmlNodeList list = doc.GetElementsByTagName("Table"); 10 for (int i = 0; i < list.Count; i++) 11 { 12 XmlNodeList chlist = list[i].ChildNodes; 13 for (int j = 0; j < chlist.Count; j++) 14 { 15 strLine = strLine + chlist[j].Name + "\t" + chlist[j].InnerText + "\n"; 16 } 17 strLine = strLine + "\n"; 18 } 19 StreamWriter sw = new StreamWriter(@"c:/c.txt"); 20 sw.WriteLine(strLine); 21 sw.Flush(); 22 sw.Close();
第二种方式:POST方式
![](http://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
1 string strUrl = "http://127.0.0.1/rss/webservice.asmx/GetNews"; 2 HttpWebRequest hwrq = (HttpWebRequest)WebRequest.Create(strUrl); 3 hwrq.Method = "POST"; 4 hwrq.ContentType = "application/x-www-form-urlencoded"; 5 String param1 = HttpUtility.UrlEncode("topNum"); 6 param1 += "=" + HttpUtility.UrlEncode("5"); 7 byte[] bt = Encoding.UTF8.GetBytes(param1); 8 hwrq.ContentLength = bt.Length; 9 Stream sw = hwrq.GetRequestStream(); 10 sw.Write(bt, 0, bt.Length); 11 sw.Close(); 12 HttpWebResponse hwrp = (HttpWebResponse)hwrq.GetResponse(); 13 Stream stream = hwrp.GetResponseStream(); 14 XmlDocument xmldoc = new XmlDocument(); 15 xmldoc.Load(stream); 16 XmlNodeList list = xmldoc.GetElementsByTagName("Table"); 17 string strLine = ""; 18 for (int i = 0; i < list.Count; i++) 19 { 20 XmlNodeList chlist = list[i].ChildNodes; 21 for (int j = 0; j < chlist.Count; j++) 22 { 23 strLine = strLine + chlist[j].Name + "\t" + chlist[j].InnerText + "\n"; 24 } 25 strLine = strLine + "\n"; 26 } 27 StreamWriter writer = new StreamWriter(@"c:\c.txt"); 28 writer.WriteLine(strLine); 29 writer.Flush(); 30 sw.Close();
get方式与post方式的主要区别在于post方式的参数要经过url编码,并在获得请求之前传送。
post的方式的参数一定要以实例中方式来写,否则会出现HTTP请求500错误。
string param1 = HttpUtility.UrlEncode("topNum");
param1+="="+HttpUtility.UrlEncode("5");
二、java直接通wsdl地址调用WebService(axis)
import java.util.Date;
import java.text.DateFormat;
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import javax.xml.namespace.QName;
import java.lang.Integer;
import javax.xml.rpc.ParameterMode;
public class caClient {
public static void main(String[] args) {
try {
String endpoint = "http://localhost:8080/ca3/services/caSynrochnized?wsdl";
//直接引用远程的wsdl文件
//以下都是套路
Service service = new Service();
Call call = (Call) service.createCall();
call.setTargetEndpointAddress(endpoint);
call.setOperationName("addUser");//WSDL里面描述的接口名称 调用的方法名
call.addParameter("userName",参数名 org.apache.axis.encoding.XMLType.XSD_DATE, 参数类型:String
javax.xml.rpc.ParameterMode.IN);//接口的参数 参数模式:'IN' or 'OUT'
call.setReturnType(org.apache.axis.encoding.XMLType.XSD_STRING);//设置返回类型
String temp = "测试人员";
String temp1 = "测试人员密码";
String result = (String)call.invoke(new Object[]{temp,temp1});
//给方法传递参数,并且调用方法 ,并获取服务器传过来的结果result
System.out.println("result is "+result);
}
catch (Exception e) {
System.err.println(e.toString());
}
}
}
参考原文链接:
http://www.cnblogs.com/hfliyi/archive/2012/09/16/2687843.html
http://www.iteye.com/problems/81358