新浪官方微博phone7 SDK采用的是基于oauth1.0授权的,虽然官方已经提供了oauth2.0的API。
今天写下新浪微博中发送带图片的微博的接口实现
"https://api.weibo.com/2/statuses/upload.json"
/// <summary>
/// 写入文件及文本
/// </summary>
/// <param name="fileData">要上传的文件内容</param>
/// <param name="filepath">上传的文件名</param>
/// <param name="txtsend">要上传的文本</param>
/// <param name="url">访问的URL</param>
public void RequestWBUrlWriterFileAndText(string fileData, string filepath, string txtsend, string url)
{
string boundary = "--------boundary";
HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);
webRequest.Method = "POST";
webRequest.ContentType = "multipart/form-data; boundary=" + boundary;
// Start the request
webRequest.BeginGetRequestStream((tReqeustStream) =>
{
HttpWebRequest request = (HttpWebRequest)tReqeustStream.AsyncState;
// End the stream request operation
Stream postStream = request.EndGetRequestStream(tReqeustStream);
var header = string.Format("--{0}", boundary);
var footer = string.Format("--{0}--", boundary);
var contentEncoding = "iso-8859-1";
var contents = new StringBuilder();
request.ContentType = string.Format("multipart/form-data; boundary={0}", boundary);
contents.AppendLine(header);
contents.AppendLine(String.Format("Content-Disposition: form-data; name=\"{0}\"", "status"));
contents.AppendLine("Content-Type: text/plain; charset=US-ASCII");
contents.AppendLine("Content-Transfer-Encoding: 8bit");
contents.AppendLine();
contents.AppendLine(txtsend);
contents.AppendLine(header);
contents.AppendLine(string.Format("Content-Disposition: form-data; name=\"{0}\"", "access_token"));
contents.AppendLine("Content-Type: text/plain; charset=US-ASCII");
contents.AppendLine("Content-Transfer-Encoding: 8bit");
contents.AppendLine();
contents.AppendLine(SdkData.Token);
contents.AppendLine(header);
string fileHeader = string.Format("Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"", "pic", filepath);
contents.AppendLine(fileHeader);
contents.AppendLine("Content-Type: application/octet-stream; charset=UTF-8");
contents.AppendLine("Content-Transfer-Encoding: binary");
contents.AppendLine();
contents.AppendLine(fileData);
contents.AppendLine(footer);
byte[] bytes = Encoding.GetEncoding(contentEncoding).GetBytes(contents.ToString());
postStream.Write(bytes, 0, bytes.Length);
postStream.Close();
// Start the web request
request.BeginGetResponse(new AsyncCallback(DownLoadTokendataReponseCallBack), request);
}, webRequest);
}
void DownLoadTokendataReponseCallBack(IAsyncResult result)
{
try
{
HttpWebRequest request = result.AsyncState as HttpWebRequest;
HttpWebResponse response = request.EndGetResponse(result) as HttpWebResponse;
Stream stream = response.GetResponseStream();
StreamReader reader = new StreamReader(stream);
string str = reader.ReadToEnd();
stream.Close();
response.Close();
if (RequestCallBack != null)
RequestCallBack(str);
}
catch (WebException ex)
{
if (ex.Status == WebExceptionStatus.UnknownError)
{
HttpStatusCode httpStatusCode = ((HttpWebResponse)ex.Response).StatusCode;
HttpWebResponse r = ((HttpWebResponse)ex.Response);
}
}
finally
{
............................
转换函数
public static string ConvertStreamToString(Stream stream)
{
if (stream == null || !stream.CanRead)
throw new ArgumentException("流为空或已释放不可读");
long d = stream.Length;
byte[] buffer = new byte[d];
stream.Read(buffer, 0, (int)d);
var contentEncoding = "iso-8859-1";
return System.Text.Encoding.GetEncoding(contentEncoding).GetString(buffer, 0, (int)d);
}
友情地址:http://www.itiask.com/space/keebai/Blog4/Post/25.aspx IT我问