读书人

腾迅开放平台微博add_pic_t求解解决思

发布时间: 2012-04-04 16:38:51 作者: rapoo

腾迅开放平台微博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"));
contents.AppendLine("Content-Type: text/plain; charset=utf-8");
//contents.AppendLine("Content-Transfer-Encoding: 8bit");


contents.AppendLine();
contents.AppendLine(content);

contents.AppendLine(header);
string fileHeader = string.Format("Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"", "pic", "pic1.jpg");
string fileData = Encoding.GetEncoding(ContentEncoding).GetString(File.ReadAllBytes(@"D:\webroot\PM3.5\Wap\nike\images\pic1.jpg"));

contents.AppendLine(fileHeader);
contents.AppendLine("Content-Type:image/jpeg; charset=" + ContentEncoding);
contents.AppendLine("Content-Transfer-Encoding: binary");
contents.AppendLine();
contents.AppendLine(fileData);
contents.AppendLine(footer);

byte[] data = Encoding.UTF8.GetBytes(contents.ToString());
httpRequest.ContentLength = data.Length;

Stream requestStream = httpRequest.GetRequestStream();
requestStream.Write(data, 0, data.Length);
requestStream.Close();

Stream responseStream;
try
{
responseStream = httpRequest.GetResponse().GetResponseStream();
}
catch (Exception e)
{
return null;
}

string stringResponse = string.Empty;
using (StreamReader responseReader = new StreamReader(responseStream, Encoding.UTF8))
{
stringResponse = responseReader.ReadToEnd();
}
responseStream.Close();
return stringResponse;
}

[解决办法]
楼主似乎搞复杂了,这个demo里面有,楼主看看吧,改成web的不难
腾讯微博开放平台SDK

读书人网 >asp.net

热点推荐