腾讯的API(add_pic_t)微博中发送文字和图片,下面的方法可以成功发送文字,但是图片无法显示,望高手点教斧正,万分感谢。

川哥编程 2014-03-13 02:33:41
腾讯的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);

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

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

sb.AppendLine(header);
string fileHeader = string.Format("Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"", "pic", "qq_female.jpg");
string fileData = Encoding.Default.GetString(File.ReadAllBytes(Server.MapPath("/ig/lovely/qq_female.jpg")));

sb.AppendLine(fileHeader);
sb.AppendLine("Content-Type:image/jpeg");
sb.AppendLine("Content-Transfer-Encoding: binary");
sb.AppendLine();
sb.AppendLine(fileData);
sb.AppendLine(footer);

byte[] data = Encoding.UTF8.GetBytes(sb.ToString());
rq.ContentLength = data.Length;

Stream stream = rq.GetRequestStream();
stream.Write(data, 0, data.Length);
stream.Close();

Stream stm;
try {
stm = rq.GetResponse().GetResponseStream();
} catch (Exception e) {
return e.ToString();
}

string result = string.Empty;
using (StreamReader reader = new StreamReader(stm, Encoding.UTF8)) {
try {
result = reader.ReadToEnd();
} finally {
reader.Close();
reader.Dispose();
}
}
stm.Close();
stm.Dispose();
return result;
}
...全文
324 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
川哥编程 2014-03-16
  • 打赏
  • 举报
回复
引用 1 楼 liyichuanZhengzhou 的回复:
up from myself
all the problems have been settled, but thank you for the same
川哥编程 2014-03-16
  • 打赏
  • 举报
回复
all the problems have been settled, but thank you for the same
川哥编程 2014-03-16
  • 打赏
  • 举报
回复
although all problem have been settled, but still thank you for your help.
机器人 2014-03-13
  • 打赏
  • 举报
回复
Encoding.Default.GetString 不对,应该是通过 Request Stream 往里写。 具体参考: http://stackoverflow.com/questions/566462/upload-files-with-httpwebrequest-multipart-form-data
川哥编程 2014-03-13
  • 打赏
  • 举报
回复
up from myself
http://blog.csdn.net/xiaoxiao108/article/details/6615848 前段时间看群里挺多讨论 微博api,正好前几天看到有个微博遥控器这东西,可以通过发微博(手机..........各种终端效果都一样)来控制电脑,看了看实现起来也不怎么复杂,整个这东西玩玩,顺便熟悉熟悉 微博api。 开发环境 vs2010 实现方法很简单 1.通过api取最新的一条微博 2.通过微博的内容控制电脑做相关操作 如:关机,取消关机,重启,摄像头截图,屏幕截图等 使用方法 1.运行QAPITool.exe后点确定(里面的appkey appsecret最好是自己在腾讯微博应用开发页面自己申请) 2.在打开的授权页面输入你的腾讯微博账号 3.把的到的授权码输入到文本框里面 4.点击开启 5.之后就可以通过你的微博输入相关命令(关机,取消关机,重启,摄像头截图,屏幕截图)来控制电脑了 具体实现代码 1.下载腾讯微博C# sdk ,有个api调试工具,方便调试使用 http://open.t.qq.com/resource.php 2.添加一个计时器,定时取最后一条微博 OauthKey oauthKey = new OauthKey(); oauthKey.customKey = appKey; oauthKey.customSecret = appSecret; oauthKey.tokenKey = accessKey; oauthKey.tokenSecret = accessSecret; string ret; UTF8Encoding utf8 = new UTF8Encoding(); statuses st = new statuses(oauthKey, "json"); ret = st.broadcast_timeline(0, 0, 1, 0); 3.解析返回json数据 JObject response = JsonConvert.DeserializeObject(str); JObject data = JsonConvert.DeserializeObject(response["data"].ToString()); JObject info = JsonConvert.DeserializeObject(data["info"].First.ToString()); string id = info["id"].ToString().Replace("\"", ""); 3.判断这个微博是不是最新发送的 从返回的数据取出 微博id 跟text 里面的数据 , 通过比较取出的微博id 跟电脑保存的id 是否一致来判断是不是新发送的 如果是新的就执行微博的控制命令,并更新保存的id StringBuilder sb = new StringBuilder(255); GetPrivateProfileString("Config", "ID", "", sb, sb.Capacity, configpath); string oldID = sb.ToString(); if (id != oldID) { string text = info["text"].ToString().Replace("\"", ""); WritePrivateProfileString("Config", "ID", id, configpath); ProcessCommand(text); } 4.通过这条微博控制电脑做相关操作 5.重启代码 Process.Start("shutdown", "-r -f -t 300"); 6.关机代码 Process.Start("shutdown", "-s -f -t 300"); 取消关机 Process.Start("shutdown", "-a"); 7.屏幕截图 var temp1 = Environment.GetEnvironmentVariable("TEMP"); var picPath1 = string.Format("{0}\\{1}.jpg", temp1, Guid.NewGuid()); Class1.GetScreen(picPath1); if (File.Exists(picPath1)) Send(picPath1); 8.摄像头截图 var temp = Environment.GetEnvironmentVariable("TEMP"); var picPath = string.Format("{0}\\{1}.jpg", temp, Guid.NewGuid()); Class1.GetCamera(picPath, this.pictureBox1); if(File.Exists(picPath)) Send(picPath); break; 9.把截取到的图片发送微博上 OauthKey oauthKey = new OauthKey(); oauthKey.customKey = appKey; oauthKey.customSecret = appSecret; oauthKey.tokenKey = accessKey; oauthKey.tokenSecret = accessSecret; t twit = new t(oauthKey, "json"); string ret; ret = twit.add_pic("pic", "127.0.0.1", "", "", file); 如果你发现有什么不合理的,需要改进的地方,联系328452421@qq.com 朱晓 。相互交流 谢谢 下载地址 顺便问下 有家是 新泰 的 有木有。 http://blog.csdn.net/xiaoxiao108/article/details/6615848

111,120

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧