关闭

一个发送xml远程请求和接收xml请求结果并置入DataSet的方法

标签: xmldatasetstringstreamencodingnull
900人阅读 评论(0) 收藏 举报
分类:

  /// <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>");
       strT.Append(DS.Tables[i].Rows[x][y].ToString ());
       strT.Append("</td>");
      }
      strT.Append("</tr>");

     }
     strT.Append("</table>");
    }
    System.Web.HttpContext.Current.Response.Write(strT.ToString());
   }
   return DS;
 
  }

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:64301次
    • 积分:1116
    • 等级:
    • 排名:千里之外
    • 原创:42篇
    • 转载:6篇
    • 译文:0篇
    • 评论:16条
    最新评论