C#CS与BS数据请求交换

来源:这里教程网 时间:2026-02-21 13:05:30 作者:

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 = @ “https://www.herecours.com/d/file/efpub/2026/21-21/20260221125259166828.jpg”;    
             //字符串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( “ 错误” );  
             }

相关推荐