腾讯的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;
}
...全文
285 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

110,534

社区成员

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

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

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