C#CS與BS資料請求交換

來源:互聯網
上載者:User
C#CS發送HTTP GET請求

try            {                WebRequest req = WebRequest.Create("http://127.0.0.1/test/loginsso.aspx?username=admin&password=admin");                req.Method = "POST";   //指定提交的Method,可以為POST和GET,一定要大寫                 //byte[] postData = System.Text.Encoding.GetEncoding("gbk").GetBytes("?username=admin&password=admin");//Post的資料                 //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");//接收的編碼                 StreamReader reader = new StreamReader(res.GetResponseStream(), resEncoding);                string html = reader.ReadToEnd();     //接收的Html                 MessageBox.Show("=========" + html);                reader.Close();                res.Close();            }            catch (Exception ex)            {                MessageBox.Show("error");            }

.NET接收GET發送請求

 Response.ContentEncoding = Encoding.GetEncoding("UTF-8");            string username = Request["username"];            string password = Request["password"];            if (username != "" && username == "admin" && password != "" && password == "admin")            {                Response.Write("success");            }            else            {                Response.Write("error" + Request.Url.Host);               // Response.Redirect("http://www.g.cn");          }

.NET接收後請求

System.Text.Encoding resEncoding = System.Text.Encoding.GetEncoding("utf-8");//接收的編碼                StreamReader reader = new StreamReader(Request.InputStream, resEncoding);                string msg = reader.ReadToEnd();                reader.Close();

C#CS發送圖片附件

C#代碼

如果 (!textBox_fileName.Text.Trim()。等於(“” ))              {                  字串 的loadFile = textBox_fileName.Text.Trim();                  字串 檔案名稱= loadFile.Substring(loadFile.LastIndexOf(“\\”)+1,loadFile.Length - 1 - loadFile.LastIndexOf(“ \\”));                  字串 urlStr = @ “http://127.0.0.1/test/UploadFile.aspx?name=”  +檔案名稱;                  UploadFileBinary(的loadFile,urlStr);              }              其他              {                  字串 alStr =  “您還沒有選擇檔案” ;                  MessageBox.Show(alStr, “ 系統提示” ,MessageBoxButtons.OK,MessageBoxIcon.Exclamation,MessageBoxDefaultButton.Button1);              }


C#代碼

公用無效 UploadFileBinary(字串 LOCALFILE,  字串 的uploadURL)            {              嘗試              {                    RDR的FileStream =  新 的FileStream(LOCALFILE,FileMode.Open);                  位元組[] = INDATA  新位元組[4096];                   INT  totbytes = 0;                  MemoryStream的POSTDATA =  新 的MemoryStream();                  INT  讀取動作= rdr.Read(INDATA,0,inData.Length);                  而 (讀取動作> 0)                  {                      postData.Write(INDATA,0,讀取動作);                      讀取動作= rdr.Read(INDATA,0,inData.Length);                      totbytes + =讀取動作;                  }                  rdr.Close();                  postData.Position = 0;                  HttpWebRequest的REQ =(HttpWebRequest的)WebRequest.Create(的uploadURL);                  req.Method =  “POST” ;                  req.ContentLength =(長)postData.Length;                  使用 (流S = req.GetRequestStream())                  {                      s.Write(postData.ToArray(),0,(INT )postData.Length);                      postData.Close();                  }                  WebResponse類RESP = req.GetResponse();                  System.Text.Encoding resEncoding = System.Text.Encoding.GetEncoding(“UTF-8”); //接收的編碼                  StreamReader的讀者=  新 的StreamReader(resp.GetResponseStream(),resEncoding);                  字串 味精= reader.ReadToEnd();                  reader.Close();                  resp.Close();                  如果 (MSG!=  空 && msg.Equals( “ 成功” ))                  {                      MessageBox.Show( “ 圖片上傳成功” ,“提示” );                  }              }              趕上 (異常前)              {                  //字串exContent;                  // exContent = ex.ToString();                  MessageBox.Show( “ 上傳失敗網路出現異常或者圖片檔案已經存在!” ,“提示” );                }            }


.NET接收圖片附件檔案

C#代碼

Response.ContentEncoding = Encoding.GetEncoding(“UTF-8” );                 //在此處放置使用者代碼以初始化頁面              位元組[]海圖=  零;              串ls_name;              如果 (Request.ServerVariables [ “REQUEST_METHOD” ]的ToString()。ToUpper的()== “POST” )              {               海圖= Request.BinaryRead(Request.ContentLength);                //擷取檔案名稱               ls_name =的Request.QueryString [ “名” ];                        //字串picName = DateTime.Now.Ticks.ToString()+符“.gif”;                 //字串picName = DateTime.Now.Ticks.ToString()+“.JPG”;               STM的FileStream =  新 的FileStream(使用Server.Mappath(一個“UploadFile /” + ls_name),System.IO.FileMode.CreateNew);               stm.Write(海圖,0,(INT )theData.Length);               stm.Close();               的Response.Write( “ 成功” );               }               其他               {                 的Response.Write( “ 錯誤” );               }
  • 相關文章

    聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    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.