httpwebrequest post方式上传图片的问题
程序功能 使用post数据的方式提交一些内容 其中就包括上传图片 程序没有报错 就是数据提交不了。贴上代码:希望高手指教或加我QQ 363455936 详谈
程序代码:
ListViewItem item = this.Sendlist.Items[sendindex]; string Category = item.SubItems[1].Text; string Title = item.SubItems[2].Text; string Content = item.SubItems[3].Text; string Pic = item.SubItems[4].Text; string boundary = "---------------------------" + DateTime.Now.Ticks.ToString("x"); WebClient client = new WebClient(); string filepath = @"C:\Users\Administrator\Downloads\image.jpg"; client.DownloadFile(Pic, filepath); //string boundary = "-----------------------------7db2be252903bc"; string poststr = "category=" + Category + "&district=luwan_xintiandi&title=" + Title + "&description=<p>" + Content + "</p>"; StringBuilder sb = new StringBuilder(); foreach (string c in poststr.Split('&')) { string[] itemdata = c.Split('='); if (itemdata. Length != 2) { break; } string name = itemdata[0]; string value = itemdata[1]; sb.Append(boundary); sb.Append("\r\n"); sb.Append("Content-Disposition: form-data; name=\"" + name + "\""); sb.Append("\r\n\r\n"); sb.Append(value); sb.Append("\r\n"); } // 文件域的数据 sb.Append(boundary); sb.Append("\r\n"); sb.Append("Content-Disposition: form-data; name=\"image\";filename=\"" +filepath + "\""); sb.Append("\r\n"); sb.Append("Content-Type: "); sb.Append("image/x-png"); sb.Append("\r\n\r\n"); string postHeader = sb.ToString(); //string postdata = boundary // + "\r\nContent-Disposition: form-data; name=\"category\"\r\n" // + "\r\n" + Category//分类 // + "\r\n" + boundary // + "\r\nContent-Disposition: form-data; name=\"district\"\r\n" // + "\r\nluwan_xintiandi"//二级分类 // + "\r\n" // + "\r\nContent-Disposition: form-data; name=\"title\"\r\n" // + "\r\n" + Title //标题 // + "\r\n-----------------------------7db2be252903bc" // + "\r\nContent-Disposition: form-data; name=\"description\"\r\n" // + "\r\n<p>" + Content + "</p>"//正文 // + "\r\n-----------------------------7db2be252903bc" // + "\r\nContent-Disposition: form-data; name=\"image\"; filename=\"" + Pic + "\""//图片 // + "\r\nContent-Type: image/x-png\r\n" // + "\r\n-----------------------------7db2be252903bc" // + "\r\nContent-Disposition: form-data; name=\"commit\"\r\n" // + "\r\nPost This Now" // + "\r\n-----------------------------7db2be252903bc--\r\n"; //byte[] data = Encoding.Default.GetBytes(postdata); HttpWebRequest wrq = (HttpWebRequest)WebRequest.Create("http://"); wrq.CookieContainer = cc; wrq.Method = "POST"; wrq.Accept = "*/*"; wrq.UserAgent = "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Tablet PC 2.0)"; wrq.ContentType = "multipart/form-data; boundary="+boundary; wrq.Referer = "http://"; byte[] postHeaderBytes = Encoding.Default.GetBytes(postHeader); //构造尾部数据 string enddata = "\r\n" + boundary + "\r\nContent-Disposition: form-data; name=\"commit\"\r\n" + "\r\nPost This Now" + "\r\n" + boundary + "--\r\n"; byte[] boundaryBytes = Encoding.Default.GetBytes(enddata); FileStream fileStream = new FileStream(filepath, FileMode.Open, FileAccess.Read); long length = postHeaderBytes.Length + fileStream.Length + boundaryBytes.Length; long leng = data.Length + fileStream.Length; wrq.ContentLength = leng; Stream requestStream = wrq.GetRequestStream(); // 输入头部数据 requestStream.Write(postHeaderBytes, 0, postHeaderBytes.Length); // 输入文件流数据 byte[] buffer = new Byte[checked((uint)Math.Min(4096, (int)fileStream.Length))]; int bytesRead = 0; while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0) requestStream.Write(buffer, 0, bytesRead); //输入尾部数据 requestStream.Write(boundaryBytes, 0, boundaryBytes.Length); //requestStream.Write(data, 0, data.Length); HttpWebResponse wrp = (HttpWebResponse)wrq.GetResponse(); string str = new StreamReader(wrp.GetResponseStream(), Encoding.Default).ReadToEnd(); wrp.Close(); } catch (Exception ex) { MessageBox.Show("发送当前信息时出错,err:" + ex.ToString()); return; }