C# 发HTTP请求

清华大佬耗费三个月吐血整理的几百G的资源,免费分享!....>>>

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
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; 
        }

如果公司设置了代理,可以这样:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
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;