string username = "fanjiaxing@hotmail.com";
string password = "XXXXXX";
string usernamePassword = username + ":" + password;
//准备调用的URL及需要POST的数据:
string url = "http://api.t.sina.com.cn/statuses/update.xml?source = 1776XXXXXX&status= test!";
//string content = "新的一天新的开始!";
//准备用于发起请求的HttpWebRequest对象
WebRequest webRequest = WebRequest.Create(url);
HttpWebRequest httpRequest = webRequest as HttpWebRequest;
//准备用于用户验证的凭据
CredentialCache myCache = new CredentialCache();
myCache.Add(new Uri(url), "Basic", new NetworkCredential(username, password));
httpRequest.Credentials = myCache;
httpRequest.Headers.Add("Authorization", "Basic " + Convert.ToBase64String(new System.Text.ASCIIEncoding().GetBytes(usernamePassword)));
//发起POST请求
httpRequest.Method = "POST";
httpRequest.ContentType = "application/x-www-form-urlencoded";
System.Text.Encoding encoding = System.Text.Encoding.ASCII;
WebResponse wr = httpRequest.GetResponse();
Stream receiveStream = wr.GetResponseStream();
using (StreamReader reader = new StreamReader(receiveStream, System.Text.Encoding.UTF8))
{
string responseContent = reader.ReadToEnd();
}
为什么WebResponse wr = httpRequest.GetResponse();
返回远程服务器返回错误: (400) 错误的请求。在线等!
我也总是遇到这个问题,但是没有高手解决啊。