新浪微薄发送

1) 准备用户验证数据:

  
  
string username = " t@cnblogs.com " ;
string password = " cnblogs.com " ;
string usernamePassword = username + " : " + password;

  username是你的微博登录用户名,password是你的博客密码。

  2) 准备调用的URL及需要POST的数据:

  
  
  
  
++++++++++++++++++++++++++++++++++++++++++++++++++++++
string news_title = " VS2010网剧合集:讲述程序员的爱情故事 " ;
int news_id = 62747 ;
string t_news = string .Format( " {0},http://news.cnblogs.com/n/{1}/ " , news_title, news_id   );
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
string data = " source=123456&status= " + System.Web.HttpUtility.UrlEncode(t_news);

4. 准备用于发起请求的HttpWebRequest对象:

  
  
System.Net.WebRequest webRequest = System.Net.WebRequest.Create(url);
System.Net.HttpWebRequest httpRequest
= webRequest as System.Net.HttpWebRequest;

5. 准备用于用户验证的凭据:

  
  
System.Net.CredentialCache myCache = new System.Net.CredentialCache();
myCache.Add(
new Uri(url), " Basic " , new System.Net.NetworkCredential(username, password));
httpRequest.Credentials
= myCache;
httpRequest.Headers.Add(
" Authorization " , " Basic " +
Convert.ToBase64String(
new System.Text.ASCIIEncoding().GetBytes(usernamePassword)));

6. 发起POST请求:

  
  
httpRequest.Method = " POST " ;
httpRequest.ContentType
= " application/x-www-form-urlencoded " ;
System.Text.Encoding encoding
= System.Text.Encoding.ASCII;
byte [] bytesToPost = encoding.GetBytes(data);
httpRequest.ContentLength
= bytesToPost.Length;
System.IO.Stream requestStream
= httpRequest.GetRequestStream();
requestStream.Write(bytesToPost,
0 , bytesToPost.Length);
requestStream.Close();

上述代码成功执行后,就会把内容发到了你的微博上了。

7. 获取服务端的响应内容:

  
  
System.Net.WebResponse wr = httpRequest.GetResponse();
System.IO.Stream receiveStream
= wr.GetResponseStream();
using (System.IO.StreamReader reader = new System.IO.StreamReader(receiveStream, System.Text.Encoding.UTF8))
{
string responseContent = reader.ReadToEnd();
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值