新浪微博oauth2.0上传图片与文本

新浪官方微博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我问

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值