HttpWebRequest的使用方法

需要在WinForm里调用一个url发送短信,为了防止自己忘记,特将HttpWebRequest的使用方法摘录如下:

 

 

 1          public   void  SendSms( string  phoneNumber,  string  text)
 2 ExpandedBlockStart.gifContractedBlock.gif         {
 3            string url;
 4            url = string.Format(@"http://a.b.c.d/path/?user=username&password=password&phonenumber={0}&text={1}&charset=gb2312", phoneNumber, HttpUtility.UrlEncode(text));
 5
 6            HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
 7
 8            request.MaximumResponseHeadersLength = 10240;
 9            request.Credentials = CredentialCache.DefaultCredentials;
10            request.Method = "GET";
11            request.Timeout = 60000;
12            WebResponse webRep = request.GetResponse();
13
14            Debug.WriteLine(webRep.Headers.ToString());
15            Debug.WriteLine(webRep.ResponseUri.OriginalString);
16
17ContractedSubBlock.gifExpandedSubBlockStart.gif            读取网页内容/不需要#region 读取网页内容/不需要
18            // Obtain a 'Stream' object associated with the response object.
19            Stream ReceiveStream = webRep.GetResponseStream();
20
21            Encoding encode = System.Text.Encoding.GetEncoding("gb2312");
22
23            // Pipe the stream to a higher level stream reader with the required encoding format. 
24            StreamReader readStream = new StreamReader(ReceiveStream, encode);
25            Console.WriteLine("\nResponse stream received");
26            Char[] read = new Char[256];
27
28            // Read 256 charcters at a time.    
29            int count = readStream.Read(read, 0256);
30            Console.WriteLine("HTML\r\n");
31
32ExpandedSubBlockStart.gifContractedSubBlock.gif            while (count > 0{
33                // Dump the 256 characters on a string and display the string onto the console.
34                String str = new String(read, 0, count);
35                Console.Write(str);
36                count = readStream.Read(read, 0256);
37            }

38
39            Console.WriteLine("");
40            // Release the resources of stream object.
41            readStream.Close();
42            #endregion

43
44            // Release the resources of response object.
45            webRep.Close();
46
47        }

转载于:https://www.cnblogs.com/CuteNet/archive/2009/03/24/1420576.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值