C#CS与BS数据请求交换


本文摘自PHP中文网,作者巴扎黑,侵删。

C#CS发送HTTP GET请求

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

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发送请求

1

2

3

4

5

6

7

8

9

10

11

12

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接收后请求

1

2

3

4

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#代码

1

2

3

4

5

6

7

8

9

10

11

12

如果 (!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#代码

阅读剩余部分

相关阅读 >>

C#基础入门-常量详解

详细介绍C#读取xml多级子节点

C#中foreach实例代码

C# 常用函数总集

C# 中guid生成格式有哪些方法

C#实现添加word文本与图片超链接的方法

关于.net(C#)正确读取中文编码文件的实例教程

C#执行原理深入解析(图文)

关于.net使用cache框架如何给程序添加cache的实例

.net core对mongodb执行多条件查询

更多相关阅读请进入《C#》频道 >>




打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...