博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C# 发HTTP请求
阅读量:4106 次
发布时间:2019-05-25

本文共 2145 字,大约阅读时间需要 7 分钟。

记录一下,调试使用

protected void btnSend_Click(object sender, EventArgs e)

        {
            string url = "http://localhost:3547/waplocation.aspx";
            string mobileNo = this.txtMobileNo.Text;
            StringBuilder reqStr = new StringBuilder(100);
            reqStr.Append("reqtype=" + txtReqType.Text + "&mobile=" + mobileNo);
            //reqStr.Append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
            //reqStr.Append("<request>");
            //reqStr.Append("<head><reqtype>" +txtReqType.Text +"</reqtype></head>");
            //reqStr.Append("<body>");
            //reqStr.Append("<mobiles>");
            //reqStr.Append("<mobile>" + mobileNo +"</mobile>");
            //reqStr.Append("</mobiles>");
            //reqStr.Append("</body>");
            //reqStr.Append("</request>");
            string postData = reqStr.ToString();
            ASCIIEncoding encoding = new ASCIIEncoding();
            byte[] data = encoding.GetBytes(postData);
            HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(url);
            
            myRequest.Method = "POST";
            myRequest.ContentType = "application/x-www-form-urlencoded";
            myRequest.ContentLength = data.Length;
            Stream newStream = myRequest.GetRequestStream();
          
            newStream.Write(data, 0, data.Length);
            newStream.Close();
            HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();
            StreamReader reader = new StreamReader(myResponse.GetResponseStream(), Encoding.Default);
            string content = reader.ReadToEnd();
            txtResult.Text = content;

        }

如果公司设置了代理,可以这样:

 try

            {
                string postData = "";
                ASCIIEncoding encoding = new ASCIIEncoding();
                byte[] data = encoding.GetBytes(postData);
                WebProxy wp = new WebProxy("proxy Address");
                wp.Credentials = new System.Net.NetworkCredential("username", "password","domain");
                HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(url);
                myRequest.Proxy = wp; 
                
                myRequest.Method = "POST";
                myRequest.ContentType = "application/x-www-form-urlencoded";
                myRequest.ContentLength = data.Length;
                Stream newStream = myRequest.GetRequestStream();
                newStream.Write(data, 0, data.Length);
                newStream.Close();
                HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();
                StreamReader reader = new StreamReader(myResponse.GetResponseStream(), Encoding.Default);
                string content = reader.ReadToEnd();
                return content;
            }
            
            catch(Exception ex){
                return string.Empty;
            }

转载地址:http://bztsi.baihongyu.com/

你可能感兴趣的文章
拉格朗日对偶问题详解
查看>>
MFC矩阵运算
查看>>
最小二乘法拟合:原理,python源码,C++源码
查看>>
ubuntu 安装mysql
查看>>
c# 计算器
查看>>
C# 简单的矩阵运算
查看>>
gcc 常用选项详解
查看>>
c++输入文件流ifstream用法详解
查看>>
c++输出文件流ofstream用法详解
查看>>
字符编码:ASCII,Unicode 和 UTF-8
查看>>
firewalld的基本使用
查看>>
Linux下SVN客户端使用教程
查看>>
i2c-tools
查看>>
Linux分区方案
查看>>
nc 命令详解
查看>>
如何使用 systemd 中的定时器
查看>>
git命令速查表
查看>>
linux进程监控和自动重启的简单实现
查看>>
OpenFeign学习(三):OpenFeign配置生成代理对象
查看>>
OpenFeign学习(四):OpenFeign的方法同步请求执行
查看>>