![](/img/trans.png)
[英]The remote server returned an error: (401) Unauthorized. Twitter oAuth
[英]Getting error : The remote server returned an error: (401) Unauthorized. , while posting my website comments to twitter. Using C#
我正在嘗試在Twitter牆上張貼我的網站評論,但出現錯誤
遠程服務器返回錯誤:(401)未經授權
我試過的代碼是:-
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();
的過程正在中斷WebResponse response = request.GetResponse();
。
任何建議都是非常可取的。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.