日期:2014-05-17  浏览次数:21091 次

c# cs与bs数据请求交换
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 接收post请求
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发送图片附件
if (!textBox_fileName.Text.Trim().Equals(""))
            {
                string loadFile = textBox_fileName.Text.Trim();
                string fileName = loadFile.Substring(loadFile.LastIndexOf("\\") + 1, loadFile.Length - 1 - loadFile.LastIndexOf("\\"));
                string urlStr = @"http://127.0.0.1/test/UploadFile.aspx?name=" + fileName;
                UploadFileBinary(loadFile, urlStr);
            }
            else
            {
                string alStr = "您还没有选择文件";
                MessageBox.Show(alStr, "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);
            }

public  void UploadFileBinary(string localFile, string uploadUrl)
        {
            try
            {

                FileStream rdr = new FileStream(localFile, FileMode.Open);
                byte[] inData = new byte[4096];
                int totbytes = 0;
                MemoryStream postData = new MemoryStream();
                int bytesRead = rdr.Read(inData, 0, inData.Length);
                while (bytesRead > 0)
                {
                    postData.Write(inData, 0, bytesRead);
                    bytesRead = rdr.Read(inData, 0, inData.Length);
                    totbytes += bytesRead;
                }
                rdr.Close();
                postData.Position = 0;
                HttpWebRequest req = (HttpWebRequest)WebRequest.Create(uploadUrl);
                req.Method = "POST";
                req.ContentLength = (long)postData.Length;
                using (Stream s = req.GetRequestStream())
                {
                    s.Write(postData.ToArray(), 0, (int)postData.Length);
                    postData.Close();
                }
                WebResponse resp = req.GetResponse();
                Sys