C#cs and BS Data request Exchange

Source: Internet
Author: User
C#cs sending an HTTP GET request

try {WebRequest req = webrequest.create ("Http://127.0.0.1/test/loginsso.aspx?username=admin&amp                ;p assword=admin "); Req.   Method = "POST"; Specify the method to be submitted, which can be post and get, be sure to capitalize//byte[] PostData = System.Text.Encoding.GetEncoding ("GBK"). GetBytes ("? Username=admin&password=admin");//post data//req.                ContentLength = Postdata.length; Stream Poststream = req.                GetRequestStream ();                Poststream.write (postdata, 0, postdata.length);                Poststream.close (); WebResponse res = req.                GetResponse ();  System.Text.Encoding resencoding = System.Text.Encoding.GetEncoding ("Utf-8");//received encoding StreamReader reader = New StreamReader (Res.                GetResponseStream (), resencoding); string html = reader.     ReadToEnd ();                Received HTML MessageBox.Show ("=========" + html); Reader.                Close (); Res.            Close ();         }   catch (Exception ex) {MessageBox.Show ("error"); }

. NET receive get send request

response.contentencoding = encoding.getencoding ("UTF-8");            String username = request["username"];            string password = request["password"];            if (username! = "" && username = = "Admin" && Password! = "" && password = = "Admin")            {                Resp Onse. Write ("Success");            }            else            {                Response.Write ("error" + Request.Url.Host);               Response.Redirect ("http://www.g.cn");          }

. NET receive after request

System.Text.Encoding resencoding = System.Text.Encoding.GetEncoding ("Utf-8");//received encoding                StreamReader reader = new StreamReader (Request.inputstream, resencoding);                String msg = reader. ReadToEnd ();                Reader. Close ();

C#cs Send picture attachment

C # code

if (! TextBox_fileName.Text.Trim (). Equals (""))              {                  string loadfile = TextBox_fileName.Text.Trim ();                  String file name = loadfile.substring (loadfile.lastindexof ("\ \") +1,loadfile.length-1-loadfile.lastindexof ("\ \"));                  String urlstr = @ "http://127.0.0.1/test/UploadFile.aspx?name="  + file name;                  Uploadfilebinary (of Loadfile,urlstr);              }              Other              {                  string alstr =  "You have not selected the file";                  MessageBox.Show (ALSTR, "system hint", messageboxbuttons.ok,messageboxicon.exclamation,messageboxdefaultbutton.button1);              }


C # code

Public invalid Uploadfilebinary (string localfile, String uploadurl) {try {RDR's F                  Ilestream = new FileStream (Localfile,filemode.open);                   byte [] = INDATA new byte [4096];                  INT totbytes = 0;                  PostData of the MemoryStream = new MemoryStream (); INT Read action = rdr.                  Read (indata,0,indata.length);                      and (Read motion > 0) {postdata.write (indata,0, read action); Read action = rdr.                      Read (indata,0,indata.length);                  Totbytes + = read action; } rdr.                  Close ();                  postdata.position = 0;                  Req of HttpWebRequest = (HttpWebRequest) webrequest.create (Uploadurl); Req.                  Method = "POST"; Req.                  ContentLength = (long) postdata.length; Use (stream s = req.       GetRequestStream ()) {S.write (Postdata.toarray (), 0, (INT) postdata.length);               Postdata.close (); } WebResponse Class resp = req.                  GetResponse (); System.Text.Encoding resencoding = System.Text.Encoding.GetEncoding ("UTF-8");                  Reader of the received encoding StreamReader = new StreamReader (Resp.getresponsestream (), resencoding); String msg = reader.                  ReadToEnd (); Reader.                  Close (); Resp.                  Close (); if (msg! = null && MSG.                  Equals ("Success")) {MessageBox.Show ("image upload succeeded", "hint");                  }} catch (before exception) {//String excontent; Excontent = ex.                  ToString (); MessageBox.Show ("Upload failed network exception or image file already exists!")                "," prompt "); }            }


. NET Receive picture attachment file

C # code

response.contentencoding = encoding.getencoding ("UTF-8");                 Place user code here to initialize page              bytes [] Nautical chart =  0;              string Ls_name;              if (Request.ServerVariables ["Request_method"] ToString (). ToUpper () = = "POST")              {               Nautical chart = request.binaryread (request.contentlength);                Get the file name               ls_name = request.querystring ["name"];                        String picname = DateTime.Now.Ticks.ToString () + character ". gif";                 String picname = DateTime.Now.Ticks.ToString () + ". JPG ";               STM filestream =  new FileStream (using Server.MapPath (a "uploadfile/" + ls_name), System.IO.FileMode.CreateNew);               Stm. Write (nautical chart, 0, (INT) thedata.length);               Stm. Close ();               Response.Write ("Success");               }               Other               {                 Response.Write ("error");               }
  • Related Article

    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.