最简单的C#调用新浪微博代码

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
string content =reader.ReadToEnd();
Response.Write(content);
3.获取微博数、粉丝数信息
public stringjson= "[{\"id\":0,\"followers_count\":0,\"friends_count\":0,\"statuses_count\":0,\"private_friends_count\":0}]"; //全局变量
}
#endregion
#region --判断字符串都是数字
public staticboolIsNumber(stringstr)
{
for (inti= 0;i<str.Length;i++)
{
if (!Char.IsNumber(str,i))
return false;
}
return true;
.WebResponsewebresponse=httpRequest.GetResponse();
System.IO.StreamreceiveStream=webresponse.GetResponseStream();
System.IO.StreamReaderreader = newSystem.IO.StreamReader(receiveStream, System.Text.Encoding.UTF8);
//发起POST请求:
httpRequest.Method= "POST";
httpRequest.ContentType= "application/x-www-form-urlencoded";
System.Text.Encodingencoding =System.Text.Encoding.ASCII;
//准备用于发起请求的HttpWebRequest对象
.WebRequestwebRequest=.WebRequest.Create(url);
.HttpWebRequesthttpRequest=.HttpWebRequest;
.WebResponsewebresponse=httpRequest.GetResponse();
System.IO.StreamreceiveStream=webresponse.GetResponseStream();
System.IO.StreamReaderreader = newSystem.IO.StreamReader(receiveStream, System.Text.Encoding.UTF8);
httpRequest.Credentials=myCache;
httpRequest.Headers.Add("Authorization", "Basic " + Convert.ToBase64String(newSystem.Text.ASCIIEncoding().GetBytes(username + ":" + password)));
.HttpWebRequesthttpRequest=.HttpWebRequest;
//准备用于用户验证的凭据:
.CredentialCachemyCache= .CredentialCache();
#region --获取参数
protectedintgetData(string type)
{
intn = 0;
json=json.Replace("[","").Replace("]","").Replace("{","").Replace("}","").Replace("\"","");
string[] v =json.Split(',');
.HttpWebRequesthttpRequest=.HttpWebRequest;
//准备用于用户验证的凭据:
.CredentialCachemyCache= .CredentialCache();
myCache.Add(new Uri(url), "Basic", workCredential(username, password));
httpRequest.Credentials=myCache;
httpRequest.Headers.Add("Authorization", "Basic " + Convert.ToBase64String(newSystem.Text.ASCIIEncoding().GetBytes(username + ":" + password)));
myCache.Add(new Uri(url), "Basic", workCredential(username, password));
httpRequest.Credentials=myCache;
httpRequest.Headers.Add("Authorization", "Basic " + Convert.ToBase64String(newSystem.Text.ASCIIEncoding().GetBytes(username + ":" + password)));
//准备用于用户验证的凭据:
.CredentialCachemyCache= .CredentialCache();
myCache.Add(new Uri(url), "Basic", workCredential(username, password));
C#调用新浪微博代码
1.写入信息
string username = "abc@"; //帐号
string password = "123456"; //密码
stringappkey= "123456789"; //Appkey密钥
//配置内容
stringurl= "/statuses/update.json";
for(inti= 0 ;i<v.Length;i++)
{
if(IsNumber(v[i].Replace(type,"").Replace(":","")))
{
n = Convert.ToInt32(v[i].Replace(type,"").Replace(":",""));
}
}
return n;
string title = TextBox1.Text.Trim();
string data = "source=" +appkey+ "&status=" +System.Web.HttpUtility.UrlEncode(title);
//准备用于发起请求的HttpWebRequest对象
.WebRequestwebRequest=.WebRequest.Create(url);
}
#endregion
#region --微博接口
protected voidgetWeibo(string username, string password, stringuids, stringappkey) //username用户名,password密码,uids用户ID,appkey密钥
requestStream.Close();
2.读取信息
string username = "abc@"; //帐号
string password = "123456"; //密码
stringappkey= "123456789"; //Appkey密钥
//配置内容
stringurl= "https:///statuses/public_timeline.json?source=" +appkey;
json=reader.ReadToEnd();
}
catch(Exception ex){ }
}
#endregion
使用:Response.Write("微博数:" +getData("statuses_count").ToString());
Response.Write("粉丝数:" +getData("followers_count").ToString());
{
try
{
//配置内容
stringurl= "https:///2/users/counts.json?uids=" +uids+ "&source=" +appkey;
//准备用于发起请求的HttpWebRequest对象
.WebRequestwebRequest=.WebRequest.Create(url);
byte[]bytesToPost=encoding.GetBytes(data);
httpRequest.ContentLength=bytesToPost.Length;
System.IO.StreamrequestStream=httpRequest.GetRequestStm.Write(bytesToPost, 0,bytesToPost.Length);
相关文档
最新文档