【发布时间】:2016-10-01 07:12:30
【问题描述】:
您好,我正在制作一个 GUI 应用程序,用户在该应用程序上提供他的 用户 ID 和密码 以在 twitter 上登录。 我有以下代码,但它对我不起作用,给出 403 Forbidden
错误StringBuilder sb = new StringBuilder();
byte[] buf = new byte[8192];
// encoding username/password
string user = Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(txtId.Text + ":" + txtPassword.Text));
//connect with verify page
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://api.twitter.com/1.1/account/verify_credentials.xml");
// HttpWebRequest request =(HttpWebRequest)WebRequest.Create("http://twitter.com/account/verify_credentials.xml");
// setting method to GET- This API expects a GET method Call
request.Method = "POST";
// setting authorization levels
request.Headers.Add("Authorization", "Basic " + user);
request.ContentType = "application/x-www-form-urlencoded;charset=UTF-8";
//request.ContentType = "application/x-www-form-urlencoded";
// set the response from the GET command
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream resStream = response.GetResponseStream();
// collect info returned from the GET call
string tempStream = null;
int count = 0;
do
{
count= resStream.Read(buf, 0, buf.Length);
if(count!=0)
{
tempStream= Encoding.ASCII.GetString(buf,0,count);
sb.Append(tempStream);
}
}while(count>0);
//convert result to XML DOC
XmlDocument doc = new XmlDocument();
doc.LoadXml(sb.ToString());
// get person name from the newly created xml document
XmlNodeList nodeList = doc.SelectNodes("/user/name");
foreach (XmlNode node in nodeList)
personName = node.InnerText;
label1.Text = "Welcome, " + personName + "!";
【问题讨论】: