打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
c# cs与bs数据请求交换
C# cs发送http get请求
C#代码
 
  1. try  
  2. {   
  3.     WebRequest req = WebRequest.Create("http://127.0.0.1/test/loginsso.aspx?username=admin&password=admin");   
  4.     req.Method = "POST";   //指定提交的Method,可以为POST和GET,一定要大写    
  5.   
  6.     //byte[] postData = System.Text.Encoding.GetEncoding("gbk").GetBytes("?username=admin&password=admin");//Post的数据    
  7.   
  8.     //req.ContentLength = postData.Length;   
  9.     Stream postStream = req.GetRequestStream();   
  10.     //postStream.Write(postData, 0, postData.Length);   
  11.     postStream.Close();   
  12.   
  13.     WebResponse res = req.GetResponse();   
  14.   
  15.     System.Text.Encoding resEncoding = System.Text.Encoding.GetEncoding("utf-8");//接收的编码    
  16.     StreamReader reader = new StreamReader(res.GetResponseStream(), resEncoding);   
  17.   
  18.     string html = reader.ReadToEnd();     //接收的Html    
  19.     MessageBox.Show("=========" + html);   
  20.   
  21.     reader.Close();   
  22.     res.Close();   
  23. }   
  24. catch (Exception ex)   
  25. {   
  26.     MessageBox.Show("error");   
  27. }  

.net 接收get发送请求
C#代码
 
  1. Response.ContentEncoding = Encoding.GetEncoding("UTF-8");   
  2.            string username = Request["username"];   
  3.            string password = Request["password"];   
  4.   
  5.            if (username != "" && username == "admin" && password != "" && password == "admin")   
  6.            {   
  7.                Response.Write("success");   
  8.            }   
  9.            else  
  10.            {   
  11.                Response.Write("error" + Request.Url.Host);   
  12.               // Response.Redirect("http://www.g.cn");   
  13.          }  

.net 接收post请求
C#代码
 
  1. System.Text.Encoding resEncoding = System.Text.Encoding.GetEncoding("utf-8");//接收的编码   
  2.                 StreamReader reader = new StreamReader(Request.InputStream, resEncoding);   
  3.                 string msg = reader.ReadToEnd();   
  4.                 reader.Close();  


c# cs发送图片附件
C#代码
 
  1. if (!textBox_fileName.Text.Trim().Equals(""))   
  2.             {   
  3.                 string loadFile = textBox_fileName.Text.Trim();   
  4.                 string fileName = loadFile.Substring(loadFile.LastIndexOf("\\") + 1, loadFile.Length - 1 - loadFile.LastIndexOf("\\"));   
  5.                 string urlStr = @"http://127.0.0.1/test/UploadFile.aspx?name=" + fileName;   
  6.                 UploadFileBinary(loadFile, urlStr);   
  7.             }   
  8.             else  
  9.             {   
  10.                 string alStr = "您还没有选择文件";   
  11.                 MessageBox.Show(alStr, "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);   
  12.             }  

C#代码
 
  1. public  void UploadFileBinary(string localFile, string uploadUrl)   
  2.         {   
  3.             try  
  4.             {   
  5.   
  6.                 FileStream rdr = new FileStream(localFile, FileMode.Open);   
  7.                 byte[] inData = new byte[4096];   
  8.                 int totbytes = 0;   
  9.                 MemoryStream postData = new MemoryStream();   
  10.                 int bytesRead = rdr.Read(inData, 0, inData.Length);   
  11.                 while (bytesRead > 0)   
  12.                 {   
  13.                     postData.Write(inData, 0, bytesRead);   
  14.                     bytesRead = rdr.Read(inData, 0, inData.Length);   
  15.                     totbytes += bytesRead;   
  16.                 }   
  17.                 rdr.Close();   
  18.                 postData.Position = 0;   
  19.                 HttpWebRequest req = (HttpWebRequest)WebRequest.Create(uploadUrl);   
  20.                 req.Method = "POST";   
  21.                 req.ContentLength = (long)postData.Length;   
  22.                 using (Stream s = req.GetRequestStream())   
  23.                 {   
  24.                     s.Write(postData.ToArray(), 0, (int)postData.Length);   
  25.                     postData.Close();   
  26.                 }   
  27.                 WebResponse resp = req.GetResponse();   
  28.                 System.Text.Encoding resEncoding = System.Text.Encoding.GetEncoding("utf-8");//接收的编码   
  29.                 StreamReader reader = new StreamReader(resp.GetResponseStream(), resEncoding);   
  30.                 string msg = reader.ReadToEnd();   
  31.                 reader.Close();   
  32.                 resp.Close();   
  33.                 if (msg != null && msg.Equals("success"))   
  34.                 {   
  35.                     MessageBox.Show("图片上传成功","提示");   
  36.                 }   
  37.             }   
  38.             catch (Exception ex)   
  39.             {   
  40.                 //string exContent;   
  41.                 // exContent = ex.ToString();   
  42.                 MessageBox.Show("上传失败!网络出现异常或者图片文件已经存在!","提示");   
  43.   
  44.             }   
  45.   
  46.         }  

.net 接收图片附件文件
C#代码
 
  1. Response.ContentEncoding = Encoding.GetEncoding("UTF-8");   
  2.   
  3.              // 在此处放置用户代码以初始化页面   
  4.             byte[] theData = null;   
  5.             String ls_name;   
  6.             if (Request.ServerVariables["REQUEST_METHOD"].ToString().ToUpper() == "POST")   
  7.             {   
  8.              theData = Request.BinaryRead(Request.ContentLength);    
  9.              //获取文件名称   
  10.              ls_name = Request.QueryString["name"];            
  11.              //string picName = DateTime.Now.Ticks.ToString() + ".gif";     
  12.              //string picName = DateTime.Now.Ticks.ToString() + ".jpg";   
  13.              FileStream stm = new FileStream(Server.MapPath("uploadfile/"+ls_name), System.IO.FileMode.CreateNew);   
  14.              stm.Write(theData, 0, (int)theData.Length);   
  15.              stm.Close();   
  16.              Response.Write("success");   
  17.              }   
  18.              else  
  19.              {   
  20.                Response.Write("error");   
  21.              }   
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
如果向某网址Post信息,并得到CookieContainer以便以后直接通过验证 - g...
C#带cookie Post和Get方式发送数据,保持cookie
C#模拟网站页面POST数据提交表单
c#与vb.net两套代码手把手教你写.net网页爬虫
C# 读写文本文件乱码解决方案
C#解决读写包含汉字的txt文件时乱码的问题
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服