A method of sending an XML remote request and receiving the results of an XML request and placing the dataset

Source: Internet
Author: User
Tags count end httpcontext return string tostring
xml| Request

<summary>
Return query Results
</summary>
<param name= "requeststring" > Requested string </param>
<param name= "WRITETEXT" > whether to print out tables </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 is the requested parameter, RequestString is the requested string

byte[] bytes = encoding.getencoding ("GB2312").  GetBytes (str); You can set the encoding
WebRequest WebRequest = WebRequest.Create (REQUESTSTR);
WebRequest. method = "POST";//type of request
WebRequest.   Timeout = 300000; Number of milliseconds to timeout
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 ("Request timed out, current string is:<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 ("Gets the returned record error, the current string is:<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 ();

Print the returned 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 + ": 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;

}




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.