标签:
public void SendWeibo( ) { try { StringBuilder data = new StringBuilder(); string url = "https://api.weibo.com/2/statuses/upload.json";//发送带图片的接口 string t_news = "测试微博--今天天气不错"; //微博 string source = "123456"; //申请得来的微博App Key string access_token = "123456Y5fE"; //申请得来的微博Access Token: data.Append("source =" + source); data.Append("&access_token =" + access_token); data.Append("&&status= =" + System.Web.HttpUtility.UrlEncode(t_news)); string filepath = @"C:\Users\Administrator\Desktop\Image\test.png"; Weibo a = new Weibo(); a.HttpUploadFile(url, data.ToString (), "file", filepath, "test.png"); } catch (Exception ex) { } }
using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Net; using System.Text; using System.Web; namespace WebWeiboTest { public class Weibo { /// <summary> /// 上传图片文件 /// </summary> /// <param name="url">提交的地址</param> /// <param name="poststr">发送的文本串 比如:user=eking&pass=123456 </param> /// <param name="fileformname">文本域的名称 比如:name="file",那么fileformname=file </param> /// <param name="filepath">上传的文件路径 比如: c:\12.jpg </param> /// <param name="filename">图片名字</param> /// <returns></returns> public string HttpUploadFile(string url, string poststr, string fileformname, string filepath, string filename) { try { // 这个可以是改变的,也可以是下面这个固定的字符串 string boundary = "---------------------------7803363818269"; // 创建request对象 HttpWebRequest webrequest = (HttpWebRequest)WebRequest.Create(url); webrequest.ContentType = "multipart/form-data; boundary=" + boundary; webrequest.Method = "POST"; // 构造发送数据 StringBuilder sb = new StringBuilder(); // 文本域的数据,将user=eking&pass=123456 格式的文本域拆分 ,然后构造 foreach (string c in poststr.Split(‘&‘)) { string[] item = c.Split(‘=‘); if (item.Length != 2) { break; } string name = item[0]; string value = item[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=\"pic\";filename="+filename); sb.Append("\r\n"); sb.Append("Content-Type: image/png"); sb.Append("\r\n\r\n"); string postHeader = sb.ToString(); byte[] postHeaderBytes = Encoding.UTF8.GetBytes(postHeader); //构造尾部数据 byte[] boundaryBytes = Encoding.UTF8.GetBytes("\r\n--" + boundary + "--\r\n"); FileStream fileStream = new FileStream(filepath, FileMode.Open, FileAccess.Read); long length = postHeaderBytes.Length + fileStream.Length + boundaryBytes.Length; webrequest.ContentLength = length; Stream requestStream = webrequest.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); WebResponse responce = webrequest.GetResponse(); Stream s = responce.GetResponseStream(); StreamReader sr = new StreamReader(s); return sr.ReadToEnd(); } catch (Exception ex) { return ""; } // 返回数据流(源码) } } }
标签:
原文地址:http://www.cnblogs.com/runsun/p/4520053.html