C# 的post应用!!!
这学期在学彩信短信wapIVR开发,怎么说呢,我个人感觉就是两个字:坑爹!
彩信上了7周实验课,但是呢,不是很感兴趣。让我郁闷的一件事就是我学的.NET方向,彩信那一块全部要用java搞,MY God,拿块豆腐砸死我吧,但是我呢又不想用java写,java都能写,C#也肯定能写,所以就花了一天时间研究了一下!
还好前段时间搞了一下android,对java勉勉强强看得懂,但是要实际开发还是比较难的。今天上午听**公司老总关于当前大学生就业的一些建议,特别是搞IT行业的,自我感觉还是要好好抓一下基础。悲催。
好了废话不多说了,进入正题。
post方法就是通过客户端的方式登录网页做些事情,摆脱浏览器(我是这样理解的,不知道对错,最近心情不太好,很是郁闷,自己去查吧),当然能做很多很多事情,这个自己去查。
好吧,直接上代码,基本上有注释:
1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel; 4 using System.Data; 5 using System.Drawing; 6 using System.Linq; 7 using System.Text; 8 using System.Windows.Forms; 9 using System.IO; 10 using System.Collections.Specialized; 11 using System.Net; 12 using ICSharpCode.SharpZipLib.Zip; 13 14 namespace MMSSend 15 { 16 public partial class Form1 : Form 17 { 18 private static string mmspath = null; 19 private static string imagepath = null; 20 private static string phone = null; 21 public Form1() 22 { 23 InitializeComponent(); 24 send.Enabled = false; 25 } 26 27 /// <summary> 28 /// 发送短信 29 /// </summary> 30 /// <param name="sender"></param> 31 /// <param name="e"></param> 32 private void send_Click(object sender, EventArgs e) 33 { 34 phone = txt_phone.Text; 35 try 36 { 37 mySubmit(ReadZIPContent()); 38 MessageBox.Show("发送成功!!!");
39 } 40 catch (Exception ex) 41 { 42 MessageBox.Show("发送失败!" + ex.ToString()); 43 } 44 45 } 46 /// <summary> 47 /// 添加图片 48 /// </summary> 49 /// <param name="sender"></param> 50 /// <param name="e"></param> 51 private void addimage_Click(object sender, EventArgs e) 52 { 53 OpenFileDialog openFileDialog = new OpenFileDialog();//打开一个对话框 54 openFileDialog.InitialDirectory = "c:\\"; 55 openFileDialog.Filter = "所有文件|*.*"; 56 openFileDialog.RestoreDirectory = true; 57 openFileDialog.FilterIndex = 1; 58 if (openFileDialog.ShowDialog() == DialogResult.OK) 59 { 60 string fname = openFileDialog.FileName;//得到文件路径 61 imagepath = fname; 62 FileStream file = new FileStream(fname, FileMode.Open, FileAccess.Read);//filestream流,主要用于将图片显示出来; 63 Bitmap picture = new Bitmap(file); 64 pictureBox1.Image = picture; 65 66 } 67 } 68 /// <summary> 69 /// 保存图片 70 /// </summary> 71 /// <param name="sender"></param> 72 /// <param name="e"></param> 73 private void save_Click(object sender, EventArgs e) 74 { 75 string filecreate = @"mms\\content.txt"; 76 77 string mmsfile = Directory.GetCurrentDirectory() + @"\mms";//得到mms文件路径 78 mmspath = mmsfile; 79 try 80 {//下面是创建txt文件并写入内容,copy图片到指定文件夹 81 if (!Directory.Exists(mmsfile)) 82 { 83 Directory.CreateDirectory(mmsfile); 84 FileStream fs = new FileStream(filecreate, FileMode.Create, FileAccess.Write); 85 StreamWriter sw = new StreamWriter(fs); 86 sw.Write(txt_content.Text); 87 sw.Close(); 88 fs.Close(); 89 File.Copy(imagepath, mmsfile + "\\image.jpg", true);//copy文件,imagepath为源地址,mmsfile为新地址 90 } 91 else 92 { 93 FileStream fs = new FileStream(filecreate, FileMode.Open, FileAccess.Write); 94 StreamWriter sw = new StreamWriter(fs); 95 sw.Write(txt_content.Text); 96 sw.Close(); 97 fs.Close(); 98 File.Copy(imagepath, mmsfile + "\\image.jpg", true); 99 } 100 101 CreateZipFile(mmsfile, @"D:\\mms.zip");//压缩文件zip 102 103 MessageBox.Show("mms.zip创建成功!!!"); 104 send.Enabled = true; 105 } 106 catch (IOException ex) 107 { 108 MessageBox.Show(ex.ToString()); 109 } 110 } 111 /// <summary> 112 /// http连接 113 /// </summary> 114 /// <param name="m_content"></param> 115 public void mySubmit(string m_content) 116 { 117 try 118 { 119 Encoding encode = System.Text.Encoding.GetEncoding("utf-8"); 120 byte[] data = Encoding.Default.GetBytes(GetPostData(ReadZIPContent())); 121 HttpWebRequest myReq = (HttpWebRequest)WebRequest.Create("网站地址,自己填写"); 122 myReq.Method = "POST"; 123 myReq.ContentType = "application/x-www-form-urlencoded"; 124 myReq.ContentLength = data.Length; 125 Stream outStream = myReq.GetRequestStream(); 126 outStream.Write(data, 0, data.Length); 127 outStream.Close(); 128 //下面的代码是用于解析返回数据,用于判断是否post数据成功,时间关系,这个请大家自己分析 129 WebResponse myResp = myReq.GetResponse(); 130 Stream ReceiveStream = myResp.GetResponseStream(); 131 StreamReader readStream = new StreamReader(ReceiveStream, encode); 132 Char[] read = new Char[256]; 133 int count = readStream.Read(read, 0, 256); 134 string str = null; 135 while (count > 0) 136 { 137 str += new String(read, 0, count); 138 count = readStream.Read(read, 0, 256); 139 //textBox2.AppendText(str); 140 } 141 readStream.Close(); 142 myResp.Close(); 143 } 144 catch (System.Exception ex) 145 { 146 147 } 148 149 } 150 151 /// <summary> 152 /// base64加密 153 /// </summary> 154 /// <returns></returns> 155 public static string ReadZIPContent() 156 { 157 String str = ""; 158 try 159 { 160 FileStream fs = new FileStream(@"D:\\mms.zip", FileMode.Open, FileAccess.Read);//读取zip文件 161 byte[] buffer2 = new byte[fs.Length]; 162 str = Convert.ToBase64String(buffer2);//base64加密 163 fs.Close(); 164 165 } 166 catch (Exception ex) 167 { 168 MessageBox.Show("base64加密时错误!"+ex.ToString()); 169 } 170 171 return str; 172 } 173 174 /// <summary> 175 /// post data 176 /// </summary> 177 /// <param name="m_content">mms.zip</param> 178 /// <returns>string</returns> 179 private string GetPostData(string m_content) 180 { 181 string strRtn = ""; 182 //下面是要post的一些数据,开始的时候用的是String.Format,但是一直不能post,不知怎么回事,也懒得研究了,在网上找了这个方法,一试就行,哈哈! 183 184 NameValueCollection nvc = new NameValueCollection(); 185 nvc.Add("userName", "用户名"); 186 nvc.Add("password", "密码"); 187 nvc.Add("phone", phone); 188 nvc.Add("serviceId", "100"); 189 nvc.Add("channelId", "1"); 190 nvc.Add("content", m_content); 191 nvc.Add("linkId", ""); 192 nvc.Add("feePhone", ""); 193 nvc.Add("priority", "9"); 194 nvc.Add("subject", txt_title.Text.ToString()); 195 nvc.Add("productId", ""); 196 nvc.Add("deliveryReport", "1"); 197 nvc.Add("sendTime", ""); 198 foreach (string ie in nvc) 199 { 200 strRtn += ie + "=" + nvc[ie] + "&"; 201 } 202 if (strRtn.EndsWith("&")) 203 strRtn = strRtn.Substring(0, strRtn.Length - 1); 204 return strRtn; ; 205 } 206 /// <summary> 207 /// 添加压缩文件 这个方法是在网上搜的,要调用一个dll:ICSharpCode.SharpZipLib 208 /// 可以自己搜一下,网上都有 209 /// </summary> 210 /// <param name="filesPath"></param> 211 /// <param name="zipFilePath"></param> 212 private static void CreateZipFile(string filesPath, string zipFilePath) 213 { 214 215 if (!Directory.Exists(filesPath)) 216 { 217 Console.WriteLine("Cannot find directory '{0}'", filesPath); 218 return; 219 } 220 221 try 222 { 223 string[] filenames = Directory.GetFiles(filesPath); 224 using (ZipOutputStream s = new ZipOutputStream(File.Create(zipFilePath))) 225 { 226 227 s.SetLevel(9); // 压缩级别 0-9 228 //s.Password = "123"; //Zip压缩文件密码 229 byte[] buffer = new byte[4096]; //缓冲区大小 230 foreach (string file in filenames) 231 { 232 ZipEntry entry = new ZipEntry(Path.GetFileName(file)); 233 entry.DateTime = DateTime.Now; 234 s.PutNextEntry(entry); 235 using (FileStream fs = File.OpenRead(file)) 236 { 237 int sourceBytes; 238 do 239 { 240 sourceBytes = fs.Read(buffer, 0, buffer.Length); 241 s.Write(buffer, 0, sourceBytes); 242 } while (sourceBytes > 0); 243 } 244 } 245 s.Finish(); 246 s.Close(); 247 } 248 } 249 catch (Exception ex) 250 { 251 Console.WriteLine("Exception during processing {0}", ex); 252 } 253 } 254 } 255 }
截个图吧:
代码写得有些乱,还有很多异常,很多问题未解决,有想法的自己弄了!
最近很是郁闷啊!!!