using
System;
using System.Data;
using System.Net;
using System.Net.Sockets;
using System.IO;
using System.Text;
using System.Xml;
using System.Threading;
using System.Collections;
using System.Runtime.InteropServices;
using System.Reflection;
using System.Drawing;
using System.Windows.Forms;
namespace Lindo.Webservice
... {
/**//// <summary>
/// 2006-3-27 舒海燕
///
/// 用于在移动设备上调用Webservice;
/// 支持直接调用和通过代理调用;
/// 能CMNET接入的无需设置代理;
/// 通过CMWAP接入的需设置代理"http://10.0.0.172:80"
/// </summary>
public class compactService2
...{
private string URL="";
private string ProxyURL="";
//private Size realSize;
// Http request/response
private HttpWebRequest m_req;
private HttpWebResponse m_resp;
// Data buffer for stream operations
private byte[] dataBuffer;
private int DataBlockSize = 256;
//private string m_Status;
//private string UpdateUrl;
public int pbVal=0, maxVal=0;
private string reStr="";
private bool state=false;
属性#region 属性
/**//// <summary>
/// 调用完成状态
/// </summary>
public bool State
...{
get
...{
return state;
}
set
...{
state=value;
}
}
/**//// <summary>
/// 返回的字符串
/// </summary>
public string ReStr
...{
get
...{
return reStr;
}
set
...{
reStr=value;
}
}
/**//// <summary>
/// webservice地址
/// </summary>
public string Url
...{
get
...{
return URL;
}
set
...{
URL=value;
}
}
/**//// <summary>
/// 代理服务器地址和端口,例如:http://10.0.0.172:80,不用为“”
/// </summary>
public string ProxyUrl
...{
get
...{
return ProxyURL;
}
set
...{
ProxyURL=value;
}
}
#endregion
webservice调用,http方式#region webservice调用,http方式
/**//// <summary>
/// 发送HTTP请求,并取得返回值
/// </summary>
/// <param name="methodName">WEB方法名</param>
/// <param name="parameters">参数</param>
/// <returns></returns>
private void Services(string methodName,object[] parameters)
...{
state=false;
reStr="";
maxVal = 0;
pbVal=0;
// Creates an HttpWebRequest with the specified URL.
string url=URL+"/"+methodName;
if(parameters!=null && parameters.Length>0)
...{
url+="?"+parameters[0].ToString();
for(int i=1;i<parameters.Length;i++)
...{
url+="&"+parameters[i].ToString();
}
}
url=url.Replace(" ","%20");
try
...{
if(ProxyURL!="")
...{
WebProxy myProxy=new WebProxy();
// Obtain the 'Proxy' of the Default browser.
Uri newUri=new Uri(ProxyURL);
// Associate the newUri object to 'myProxy' object so that new myProxy settings can be set.
myProxy.Address=newUri;
// Create a NetworkCredential object and associate it with the Proxy property of request object.
m_req.Proxy=myProxy;
}
m_req = (HttpWebRequest)HttpWebRequest.Create(url);
m_req.BeginGetResponse(new AsyncCallback(ResponseReceived), null);
}
catch
...{
state=true;
}
}
private void AllDone()
...{
try
...{
Abort();
reStr=Invok(reStr.ToString().Replace("<","<").Replace(">",">"));
}
catch
...{
}
state=true;
}
// Asynchronous routine to process the http web request
void ResponseReceived(IAsyncResult res)
...{
// Try getting the web response. If there was an error (404 or other),
// web exception will be thrown hete
try
...{
m_resp = (HttpWebResponse)m_req.EndGetResponse(res);
}
catch//(WebException ex)
...{
state=true;
return;
}
dataBuffer = new byte[ DataBlockSize ];
// Prepare the propgres bar
pbVal=0;
maxVal = (int)m_resp.ContentLength;
//pbProgress.Invoke(new EventHandler(SetProgressMax));
// Start reading from network stream asynchronously
m_resp.GetResponseStream().BeginRead(dataBuffer, 0, DataBlockSize, new AsyncCallback(OnDataRead), this);
}
// Asynchronous network stream reading
void OnDataRead(IAsyncResult res)
...{
// Get bytecount of the received chunk
int nBytes = m_resp.GetResponseStream().EndRead(res);
// Dump it to the output stream
pbVal += nBytes;
string s=Encoding.UTF8.GetString(dataBuffer,0,nBytes);
reStr+=s;
//dataBuffer=new byte[DataBlockSize];
//pbProgress.Invoke(new EventHandler(UpdateByte));
if ( nBytes > 0 )
...{
// If read was successful, continue reading asynchronously as there is more data
m_resp.GetResponseStream().BeginRead(dataBuffer, 0, DataBlockSize, new AsyncCallback(OnDataRead), this);
}
else
...{
// Otherwise close the stream and proceed with installation
AllDone();
}
}
/**//// <summary>
/// 判断返回的数据类型,并作一些初步处理
/// </summary>
/// <param name="XMLstring"></param>
/// <returns></returns>
private string Invok(string XMLstring)
...{
string obj=XMLstring;
int i;
i=obj.IndexOf("<?xml version="1.0" encoding="utf-8"?> <string xmlns="http://tempuri.org/">");
if(i>=0)
...{
obj=obj.Substring(76,obj.Length-85);
return obj;
}
i=obj.IndexOf("<?xml version="1.0" encoding="utf-8"?> <boolean xmlns="http://tempuri.org/">");
if(i>=0)
...{
obj=obj.Substring(77,obj.Length-87);
return obj;
}
//i=obj.IndexOf("<?xml version="1.0" encoding="utf-8"?> <DataSet xmlns="http://tempuri.org/">");
//if(i>=0)
//{
//obj=obj.Substring(76,obj.Length-85);
//return obj;
//}
i=obj.IndexOf("<?xml version="1.0" encoding="utf-8"?> <base64Binary xmlns="http://tempuri.org/">");
if(i>=0)
...{
obj=obj.Substring(82,obj.Length-97);
return obj;
}
return obj;
}
#endregion
方法#region 方法
/**//// <summary>
/// 调用webservice
/// </summary>
/// <param name="methodName">WEB方法</param>
/// <returns></returns>
public void ServiceString(string methodName)
...{
Services(methodName,null);
}
/**//// <summary>
/// 调用webservice
/// </summary>
/// <param name="methodName">WEB方法</param>
/// <param name="parameters">参数,(参数名=值)</param>
/// <returns></returns>
public void ServiceString(string methodName,object[] parameters)
...{
Services(methodName,parameters);
}
public void Abort()
...{
try
...{
m_req.Abort();
m_resp.Close();
}
catch
...{
}
}
#endregion
using System.Data;
using System.Net;
using System.Net.Sockets;
using System.IO;
using System.Text;
using System.Xml;
using System.Threading;
using System.Collections;
using System.Runtime.InteropServices;
using System.Reflection;
using System.Drawing;
using System.Windows.Forms;
namespace Lindo.Webservice
... {
/**//// <summary>
/// 2006-3-27 舒海燕
///
/// 用于在移动设备上调用Webservice;
/// 支持直接调用和通过代理调用;
/// 能CMNET接入的无需设置代理;
/// 通过CMWAP接入的需设置代理"http://10.0.0.172:80"
/// </summary>
public class compactService2
...{
private string URL="";
private string ProxyURL="";
//private Size realSize;
// Http request/response
private HttpWebRequest m_req;
private HttpWebResponse m_resp;
// Data buffer for stream operations
private byte[] dataBuffer;
private int DataBlockSize = 256;
//private string m_Status;
//private string UpdateUrl;
public int pbVal=0, maxVal=0;
private string reStr="";
private bool state=false;
属性#region 属性
/**//// <summary>
/// 调用完成状态
/// </summary>
public bool State
...{
get
...{
return state;
}
set
...{
state=value;
}
}
/**//// <summary>
/// 返回的字符串
/// </summary>
public string ReStr
...{
get
...{
return reStr;
}
set
...{
reStr=value;
}
}
/**//// <summary>
/// webservice地址
/// </summary>
public string Url
...{
get
...{
return URL;
}
set
...{
URL=value;
}
}
/**//// <summary>
/// 代理服务器地址和端口,例如:http://10.0.0.172:80,不用为“”
/// </summary>
public string ProxyUrl
...{
get
...{
return ProxyURL;
}
set
...{
ProxyURL=value;
}
}
#endregion
webservice调用,http方式#region webservice调用,http方式
/**//// <summary>
/// 发送HTTP请求,并取得返回值
/// </summary>
/// <param name="methodName">WEB方法名</param>
/// <param name="parameters">参数</param>
/// <returns></returns>
private void Services(string methodName,object[] parameters)
...{
state=false;
reStr="";
maxVal = 0;
pbVal=0;
// Creates an HttpWebRequest with the specified URL.
string url=URL+"/"+methodName;
if(parameters!=null && parameters.Length>0)
...{
url+="?"+parameters[0].ToString();
for(int i=1;i<parameters.Length;i++)
...{
url+="&"+parameters[i].ToString();
}
}
url=url.Replace(" ","%20");
try
...{
if(ProxyURL!="")
...{
WebProxy myProxy=new WebProxy();
// Obtain the 'Proxy' of the Default browser.
Uri newUri=new Uri(ProxyURL);
// Associate the newUri object to 'myProxy' object so that new myProxy settings can be set.
myProxy.Address=newUri;
// Create a NetworkCredential object and associate it with the Proxy property of request object.
m_req.Proxy=myProxy;
}
m_req = (HttpWebRequest)HttpWebRequest.Create(url);
m_req.BeginGetResponse(new AsyncCallback(ResponseReceived), null);
}
catch
...{
state=true;
}
}
private void AllDone()
...{
try
...{
Abort();
reStr=Invok(reStr.ToString().Replace("<","<").Replace(">",">"));
}
catch
...{
}
state=true;
}
// Asynchronous routine to process the http web request
void ResponseReceived(IAsyncResult res)
...{
// Try getting the web response. If there was an error (404 or other),
// web exception will be thrown hete
try
...{
m_resp = (HttpWebResponse)m_req.EndGetResponse(res);
}
catch//(WebException ex)
...{
state=true;
return;
}
dataBuffer = new byte[ DataBlockSize ];
// Prepare the propgres bar
pbVal=0;
maxVal = (int)m_resp.ContentLength;
//pbProgress.Invoke(new EventHandler(SetProgressMax));
// Start reading from network stream asynchronously
m_resp.GetResponseStream().BeginRead(dataBuffer, 0, DataBlockSize, new AsyncCallback(OnDataRead), this);
}
// Asynchronous network stream reading
void OnDataRead(IAsyncResult res)
...{
// Get bytecount of the received chunk
int nBytes = m_resp.GetResponseStream().EndRead(res);
// Dump it to the output stream
pbVal += nBytes;
string s=Encoding.UTF8.GetString(dataBuffer,0,nBytes);
reStr+=s;
//dataBuffer=new byte[DataBlockSize];
//pbProgress.Invoke(new EventHandler(UpdateByte));
if ( nBytes > 0 )
...{
// If read was successful, continue reading asynchronously as there is more data
m_resp.GetResponseStream().BeginRead(dataBuffer, 0, DataBlockSize, new AsyncCallback(OnDataRead), this);
}
else
...{
// Otherwise close the stream and proceed with installation
AllDone();
}
}
/**//// <summary>
/// 判断返回的数据类型,并作一些初步处理
/// </summary>
/// <param name="XMLstring"></param>
/// <returns></returns>
private string Invok(string XMLstring)
...{
string obj=XMLstring;
int i;
i=obj.IndexOf("<?xml version="1.0" encoding="utf-8"?> <string xmlns="http://tempuri.org/">");
if(i>=0)
...{
obj=obj.Substring(76,obj.Length-85);
return obj;
}
i=obj.IndexOf("<?xml version="1.0" encoding="utf-8"?> <boolean xmlns="http://tempuri.org/">");
if(i>=0)
...{
obj=obj.Substring(77,obj.Length-87);
return obj;
}
//i=obj.IndexOf("<?xml version="1.0" encoding="utf-8"?> <DataSet xmlns="http://tempuri.org/">");
//if(i>=0)
//{
//obj=obj.Substring(76,obj.Length-85);
//return obj;
//}
i=obj.IndexOf("<?xml version="1.0" encoding="utf-8"?> <base64Binary xmlns="http://tempuri.org/">");
if(i>=0)
...{
obj=obj.Substring(82,obj.Length-97);
return obj;
}
return obj;
}
#endregion
方法#region 方法
/**//// <summary>
/// 调用webservice
/// </summary>
/// <param name="methodName">WEB方法</param>
/// <returns></returns>
public void ServiceString(string methodName)
...{
Services(methodName,null);
}
/**//// <summary>
/// 调用webservice
/// </summary>
/// <param name="methodName">WEB方法</param>
/// <param name="parameters">参数,(参数名=值)</param>
/// <returns></returns>
public void ServiceString(string methodName,object[] parameters)
...{
Services(methodName,parameters);
}
public void Abort()
...{
try
...{
m_req.Abort();
m_resp.Close();
}
catch
...{
}
}
#endregion