日期:2014-05-16  浏览次数:20791 次

腾讯的API(add_pic_t)微博中发送文字和图片,下面的方法可以成功发送文字,但是图片无法显示,望高手点教斧正,万分感谢。
腾讯的API(add_pic_t)微博中发送文字和图片,下面的方法可以成功发送文字,但是图片无法显示,望高手点教斧正,万分感谢。
pic参数描述:要上传的图片的文件名以及图片的内容(在发送请求时,图片内容以二进制数据流的形式发送,见下面的请求示例)。图片仅支持gif、jpeg、jpg、png、bmp及ico格式(所有图片都会重新压缩,gif被重新压缩后不会再有动画效果),图片size小于4M。

add_pic_t:  http://wiki.connect.qq.com/add_pic_t

 public string AddPicToTencentWeibo(string access_token, string oauth_consumer_key, string openid, string content, string url) {
            WebRequest req = WebRequest.Create(url);
            HttpWebRequest rq = req as HttpWebRequest;
            if (rq == null) {
                return null;
            }
            rq.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.2; .NET CLR 1.1.4322; .NET CLR 2.0.50727)";
            //httpRequest.ContentType = "application/x-www-form-urlencoded";
            rq.Method = "POST";

            string boundary = Guid.NewGuid().ToString();
            string header = string.Format("--{0}", boundary);
            string footer = string.Format("--{0}--", boundary);

            var sb = new StringBuilder();
            rq.ContentType = string.Format("multipart/form-data; boundary={0}", boundary);
            sb.AppendLine(header);
            sb.AppendLine(String.Format("Content-Disposition: form-data; name=\"{0}\"", "access_token"));
            sb.AppendLine("Content-Type: text/plain; charset=utf-8");
            //contents.AppendLine("Content-Transfer-Encoding: 8bit");
            sb.AppendLine();
            sb.AppendLine(access_token);

            sb.AppendLine(header);
            sb.AppendLine(string.Format("Content-Disposition: form-data; name=\"{0}\"", "oauth_consumer_key"));
            sb.AppendLine("Content-Type: text/plain; charset=utf-8");
            //contents.AppendLine("Content-Transfer-Encoding: 8bit");
            sb.AppendLine();
            sb.AppendLine(oauth_consumer_key);