- 爱易网页
-
ASP.NET教程
- 腾迅开放平台微博add_pic_t求解解决思路
日期:2014-05-18 浏览次数:20398 次
腾迅开放平台微博add_pic_t求解
最近在弄一个腾迅开放平台微博分享(http://wiki.opensns.qq.com/wiki/%E3%80%90QQ%E7%99%BB%E5%BD%95%E3%80%91API%E6%96%87%E6%A1%A3)的功能。其中单独发一条文字微博(add_t)已经测试成功了,然后发图片和文字微博的方法(add_pic_t)遇到了问题不知道如何解决。
其中测试通过的add_t方法,我的做法如下:
string postData = "access_token=" + access_token + "&oauth_consumer_key=" + clientid + "&openid=" + openid + "&format=json&content=" + System.DateTime.Now.ToString();
request_ret = CustomerAD.CommonLibrary.Http.PostDataToUrl(postData, "https://graph.qq.com/t/add_t");
相关的key已经申请有效的了,我拼凑了相关的参数后post过去就成功了。现在关键的问题是方法add_pic_t的必备参数除了content外还有一个参数pic,要命的是这个pic是binary类型,我之前遇到的都是字符串类型的,这里就不知道怎么处理了,用了不少方法都失败了。
希望论坛的大哥大姐们指点一下,不胜感激,谢谢
附上我自己测试的方法,返回是提示没有任何错误,成功的发表了文字微博,但是图片没有出来。
/// <summary>
/// Post数据到远程地址
/// </summary>
/// <param name="data"></param>
/// <param name="url"></param>
/// <returns></returns>
public static string PostDataToUrl(string access_token,string oauth_consumer_key,string openid,string content,string imgPath,string url)
{
WebRequest request = WebRequest.Create(url);
HttpWebRequest httpRequest = request as HttpWebRequest;
if (httpRequest == null)
{
return null;
}
httpRequest.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";
httpRequest.Method = "POST";
string boundary = Guid.NewGuid().ToString();
string header = string.Format("--{0}", boundary);
string footer = string.Format("--{0}--", boundary);
var contents = new StringBuilder();
httpRequest.ContentType = string.Format("multipart/form-data; boundary={0}", boundary);
contents.AppendLine(header);
contents.AppendLine(String.Format("Content-Disposition: form-data; name=\"{0}\"", "access_token"));
contents.AppendLine("Content-Type: text/plain; charset=utf-8");
//contents.AppendLine("Content-Transfer-Encoding: 8bit");
contents.AppendLine();
contents.AppendLine(access_token);
contents.AppendLine(header);
contents.AppendLine(string.Format("Content-Disposition: form-data; name=\"{0}\"", "oauth_consumer_key"));
contents.AppendLine("Content-Type: text/plain; charset=utf-8");
//contents.AppendLine("Content-Transfer-Encoding: 8bit");
contents.AppendLine();
contents.AppendLine(oauth_consumer_key);
contents.AppendLine(header);
contents.AppendLine(string.Format("Content-Disposition: form-data; name=\"{0}\"", "openid"));
contents.AppendLine("Content-Type: text/plain; charset=utf-8");
//contents.AppendLine("Content-Transfer-Encoding: 8bit");
contents.AppendLine();
contents.AppendLine(openid);
contents.AppendLine(header);
contents.AppendLine(string.Format("Content-Disposition: form-data; name=\"{0}\"", "content"));