通过COM组件测试webService地址是否能够访问
//适用于测试连接webService远程服务器
//引用Microsoft XML, v3.0 COM组件
//测试连接
privatevoid btnTestConn_Click(object sender, EventArgs e) {
string strIP = txtServerAdddress.Text;
if (!strIP.Contains("http://"))
{
strIP = "http://" + strIP;
}
string strProt = txtServerProt.Text;
string strAddress = strIP + ":" + strProt +
"/DataService?wsdl";
string urls = "http://192.168.1.7:8884/DataService?wsdl"; XMLHTTP http = new XMLHTTP();
try
{
http.open("GET", strAddress, false, null, null);
http.send(strAddress);
int status = http.status;
if (status == 200)
{
MessageBox.Show("成功");
}
else
{
MessageBox.Show("连接失败。
状态:" + status.ToString());
}
}
catch
{
MessageBox.Show("连接失败!");
}
}
二、测试webService是否访问正常
//测试连接
privatevoid btnTestConn_Click(object sender, EventArgs e) {
string strIP = txtServerAdddress.Text;
if (!strIP.Contains("http://"))
{
strIP = "http://" + strIP;
}
string strProt = txtServerProt.Text;
string strUrl = strIP + ":" + strProt + "/DataService?wsdl"; //string strUrl =
"http://192.168.1.7:8884/DataService?wsdl";
bool isConn = tools.TestWebserviceConn(strUrl);
if (isConn)
{
MessageBox.Show("连接成功!");
}
else
{
MessageBox.Show("连接失败!");
}
}
///<summary>
///链接webservice
///</summary>
///<param name="strUrl">URL地址</param>
///<returns>true 成功,false 失败</returns> publicstaticbool TestWebserviceConn(string strUrl)
{
bool isConn = true;
try
{
HttpWebRequest webrequest =
(HttpWebRequest)HttpWebRequest.Create(strUrl); HttpWebResponse webreponse = (HttpWebResponse)webrequest.GetResponse();
Stream stream = webreponse.GetResponseStream();
//save data in the stream
byte[] rsByte = new Byte[webreponse.ContentLength]; stream.Read(rsByte, 0, (int)webreponse.ContentLength); string strRetun
=System.Text.Encoding.UTF8.GetString(rsByte, 0, rsByte.Length).ToString();
}
catch
{
isConn = false; }
return isConn; }。