分数不多,请见谅。今天按照网上的方法,写了一段代码,将内容发布至新浪微博,代码如下,返回的响应结果一直都是:The remote server returned an error: (403) Forbidden.
并且内容没有发布上去,请大家帮忙看下原因,多谢!protected void Page_Load(object sender, EventArgs e)
{
//1. 准备用户验证数据
string username = "wang********@yahoo.com.cn";
string password = "*******";
string usernamePassword = username + ":" + password; //2. 准备调用的URL及需要POST的数据
string url = "http://api.t.sina.com.cn/statuses/update.xml";
string news_title = "测试发布";
string t_news = string.Format("{0},http://weibo.com/22683*****/", news_title);
string data = "source=36133*****&status=" + System.Web.HttpUtility.UrlEncode(t_news); //3. 准备用于发起请求的HttpWebRequest对象
System.Net.WebRequest webRequest = System.Net.WebRequest.Create(url);
System.Net.HttpWebRequest httpRequest = webRequest as System.Net.HttpWebRequest;
//4. 准备用于用户验证的凭据
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)));
//5. 发起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();
string responseContent = "";
try
{
//获取服务端的响应内容
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))
{
responseContent = reader.ReadToEnd();
}
}
catch (Exception ex)
{
responseContent = ex.Message;
}
Response.Write(responseContent); }
并且内容没有发布上去,请大家帮忙看下原因,多谢!protected void Page_Load(object sender, EventArgs e)
{
//1. 准备用户验证数据
string username = "wang********@yahoo.com.cn";
string password = "*******";
string usernamePassword = username + ":" + password; //2. 准备调用的URL及需要POST的数据
string url = "http://api.t.sina.com.cn/statuses/update.xml";
string news_title = "测试发布";
string t_news = string.Format("{0},http://weibo.com/22683*****/", news_title);
string data = "source=36133*****&status=" + System.Web.HttpUtility.UrlEncode(t_news); //3. 准备用于发起请求的HttpWebRequest对象
System.Net.WebRequest webRequest = System.Net.WebRequest.Create(url);
System.Net.HttpWebRequest httpRequest = webRequest as System.Net.HttpWebRequest;
//4. 准备用于用户验证的凭据
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)));
//5. 发起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();
string responseContent = "";
try
{
//获取服务端的响应内容
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))
{
responseContent = reader.ReadToEnd();
}
}
catch (Exception ex)
{
responseContent = ex.Message;
}
Response.Write(responseContent); }
============================
请问这句是啥意思?