2 guoshaolong1 guoshaolong1 于 2014.04.22 11:54 提问

怎么调用新浪微博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();
        }
Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!