问题描述
- 怎么调用新浪微博API开放接口发图片微博
-
我的代码如下,发文字的可以,发图片微博却不行,怎么总是返回400,求解答,他要求图片需要传入binary类型
string url = "https://upload.api.weibo.com/2/statuses/upload.json";
string usernamePassword = UserName + ":" + PassWord;
string t_news = string.Format("{0},{1}", NewsTitle, NewsContent);FileStream fs = new FileStream( "E:\gsl\QQ130.png", FileMode.Open, System.IO.FileAccess.Read, FileShare.ReadWrite); int streamLength = (int)fs.Length; byte[] image = new byte[streamLength]; fs.Read(image, 0, streamLength); fs.Close(); string data = "source=" + AppKey + "&status=" + System.Web.HttpUtility.UrlEncode(t_news) + "&pic=" + System.Text.Encoding.Default.GetString(image); System.Net.WebRequest webRequest = System.Net.WebRequest.Create(url); System.Net.HttpWebRequest httpRequest = webRequest as System.Net.HttpWebRequest; System.Net.CredentialCache myCache = new System.Net.CredentialCache(); myCache.Add(new Uri(url), "Basic", new System.Net.NetworkCredential(UserName, PassWord)); httpRequest.Credentials = myCache; httpRequest.Headers.Add("Authorization", "Basic " + Convert.ToBase64String(new System.Text.ASCIIEncoding().GetBytes(usernamePassword))); httpRequest.Method = "POST"; httpRequest.ContentType = "multipart/form-data"; System.Text.Encoding encoding = System.Text.Encoding.ASCII; byte[] bytesToPost = encoding.GetBytes(data); httpRequest.ContentLength = bytesToPost.Length; System.IO.Stream requestStream = httpRequest.GetRequestStream(); requestStream.Write(bytesToPost, 0, bytesToPost.Length); requestStream.Close(); System.Net.WebResponse wr = httpRequest.GetResponse(); System.IO.Stream receiveStream = wr.GetResponseStream(); using (System.IO.StreamReader reader = new System.IO.StreamReader(receiveStream, System.Text.Encoding.UTF8)) { string responseContent = reader.ReadToEnd(); }
时间: 2024-11-02 21:01:27