日期:2014-04-10 浏览次数:20956 次
/// <summary>
/// 前往查询结果
/// </summary>
/// <param name="RequestString">请求的字符串</param>
/// <param name="WriteText">能否打印出表格</param>
/// <returns></returns>
public static System.Data.DataSet GetDataSetByRequest(string RequestString,bool WriteText)
{
String requestStr="http://192.168.1.1/post.aspx";
String str="request="+RequestString; //request为请求的参数,RequestString是请求的字符串
byte[] bytes = Encoding.GetEncoding("GB2312").GetBytes(str); //可以设置编码
WebRequest webrequest = WebRequest.Create(requestStr);
webrequest.Method = "POST";//请求的类型
webrequest.Timeout = 300000; //超时的毫秒数
webrequest.ContentType="application/x-www-form-urlencoded";
System.Data.DataSet DS = new DataSet();
webrequest.ContentLength=bytes.Length;
Stream oStreamOut = null;
try
{
oStreamOut = webrequest.GetRequestStream();
}
catch
{
webrequest.Abort();
DS.Dispose();
System.Web.HttpContext.Current.Response.Write("请求超时,当前的字符串为:<br>" + System.Web.HttpContext.Current.Server.HtmlEncode(RequestString));
System.Web.HttpContext.Current.Response.End();
}
finally
{
}
oStreamOut.Write(bytes,0,bytes.Length);
oStreamOut.Close();
WebResponse myWebResponse = null;
try
{
myWebResponse = webrequest.GetResponse();
}
catch
{
webrequest.Abort();
DS.Dispose();
System.Web.HttpContext.Current.Response.Write("获取前往的记录出错,当前的字符串为:<br>" + System.Web.HttpContext.Current.Server.HtmlEncode(RequestString));
System.Web.HttpContext.Current.Response.End();
}
Stream streamResponse=myWebResponse.GetResponseStream();
Encoding encode = Encoding.GetEncoding("GB2312");
StreamReader streamRead = new StreamReader(streamResponse, encode);
System.Xml.XmlTextReader Reader = new System.Xml.XmlTextReader(streamRead);
DS.ReadXml(Reader);
Reader.Close();
streamResponse.Close();
streamRead.Close();
myWebResponse.Close();
//Console.Read();
打印前往的Datable
if(WriteText)
{
System.Text.StringBuilder strT = new StringBuilder();
for(int i=0;i<DS.Tables.Count;i++)
{
strT.Append("<br>Table" + i.ToString() + " " + DS.Tables[i].TableName + ":<hr><Table width=100% border=1>");
for(int x=0;x<DS.Tables[i].Rows.Count;x++)
{
strT.Append("<tr>");
for(int y=0;y<DS.Tables[i].Columns.Count;y++)
{
strT.Append("<td>");
strT.Append(DS.Tables[i].Columns[y].ColumnName.ToString() + ":<br>");