我试图在我的推特墙上发布我的网站评论,但我收到了一个错误
远程服务器返回错误:(401) Unauthorized
我尝试过的代码如下:-
using System;
using System.Web;
using System.Net;
using System.IO;
using System.Web.Script.Serialization;
using System.Text;
using System.Configuration;
namespace Dotned.UI.Framework
{
public class TwitterClient
{
public string Username { get; set; }
public string Password { get; set; }
public Exception Error { get; set; }
private string _twitterUpdateUrl = "http://twitter.com/statuses/update.json";
public TwitterClient(string userName, string password)
{
this.Username = userName;
this.Password = password;
}
public void SendMessage(string message)
{
try
{
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(_twitterUpdateUrl);
request.Credentials = new NetworkCredential(this.Username, this.Password);
SetRequestParams(request);
string post = string.Format("status={0}", HttpUtility.UrlEncode(message));
using (Stream requestStream = request.GetRequestStream())
{
using (StreamWriter writer = new StreamWriter(requestStream))
{
writer.Write(post);
}
}
WebResponse response = request.GetResponse();
string content;
using (Stream responseStream = response.GetResponseStream())
{
using (StreamReader reader = new StreamReader(responseStream))
{
content = reader.ReadToEnd();
}
}
}
catch (Exception ex)
{
Error = ex;
}
}
private static void SetRequestParams(HttpWebRequest request)
{
System.Net.ServicePointManager.Expect100Continue = false;
request.Timeout = 50000;
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
}
}
}
该过程正在中断 WebResponse response = request.GetResponse();
。
任何建议都是非常值得赞赏的。