C#的Post应用2

前段时间写了一个C#发彩信的,后来也写了一个发短信的。直接上代码,没什么区别!

 1  /// <summary>
 2         /// 拼接字符串
 3         /// </summary>
 4         /// <param name="m_content"></param>
 5         /// <returns></returns>
 6         public string commString(string m_content)
 7         {
 8             string strRtn = "";
 9             NameValueCollection nvc = new NameValueCollection();
10             nvc.Add("userName", "userNmae");
11             nvc.Add("password", "123456");
12             nvc.Add("phone", "你的电话号码");
13             nvc.Add("destNumber", "1");
14             nvc.Add("serviceId", "158");
15             nvc.Add("channelId", "1");
16 
17             nvc.Add("linkId", "");
18             nvc.Add("moId", "");
19             nvc.Add("spNumber", "");
20             nvc.Add("content", m_content);
21             nvc.Add("functionType", "");
22             nvc.Add("type", "");
23             nvc.Add("", "3");
24             foreach (string ie in nvc)
25             {
26                 strRtn += ie + "=" + nvc[ie] + "&";
27             }
28             if (strRtn.EndsWith("&"))
29                 strRtn = strRtn.Substring(0, strRtn.Length - 1);
30             return strRtn;
31         }
32 
33         public void requestUrl(string commString)
34         {
35             try
36             {
37                 Encoding encode = System.Text.Encoding.GetEncoding("UTF-8");
38                 byte[] data = encode.GetBytes(commString);
39                 HttpWebRequest myReq = (HttpWebRequest)WebRequest.Create("发短信地址");
40                 myReq.Method = "POST";
41                 myReq.ContentType = "application/x-www-form-urlencoded";
42                 myReq.ContentLength = data.Length;
43                 Stream outStream = myReq.GetRequestStream();
44                 outStream.Write(data, 0, data.Length);
45                 outStream.Close();
46 
47                 WebResponse myResp = myReq.GetResponse();
48                 Stream ReceiveStream = myResp.GetResponseStream();
49                 StreamReader readStream = new StreamReader(ReceiveStream, encode);
50                 Char[] read = new Char[256];
51                 int count = readStream.Read(read, 0, 256);
52                 string str = null;
53                 while (count > 0)
54                 {
55                     str += new String(read, 0, count);
56                     count = readStream.Read(read, 0, 256);
57                     txt_content.AppendText(str);
58                 }
59                 readStream.Close();
60                 myResp.Close();
61             }
62             catch (System.Exception ex)
63             {
64                 txt_content.AppendText("RequestError" + ex.ToString());
65             }
66         }
67 
68         private void button1_Click(object sender, EventArgs e)
69         {
70             try
71             {
72                 requestUrl(commString(txt_phone.Text));
73             }
74             catch (Exception ex)
75             {
76                 txt_content.AppendText("SendError" + ex.ToString());
77             }
78         }
posted @ 2012-06-09 21:41  honging  阅读(202)  评论(0编辑  收藏  举报