C# 获取+;用c语言编写的网页的1#
我想要一个方法来获取网页url,并返回它的计数为+1。我在谷歌上搜索了一下,找到的方法是:C# 获取+;用c语言编写的网页的1#,c#,.net,c#-4.0,google-plus,google-plus-one,C#,.net,C# 4.0,Google Plus,Google Plus One,我想要一个方法来获取网页url,并返回它的计数为+1。我在谷歌上搜索了一下,找到的方法是: int GetPlusOnes(string url) { string googleApiUrl = "https://clients6.google.com/rpc"; //?key=AIzaSyCKSbrvQasunBoV16zDH9R33D88CeLr9gQ"; string postData = @"[{""method"":""pos.plusones.get"","
int GetPlusOnes(string url)
{
string googleApiUrl = "https://clients6.google.com/rpc"; //?key=AIzaSyCKSbrvQasunBoV16zDH9R33D88CeLr9gQ";
string postData = @"[{""method"":""pos.plusones.get"",""id"":""p"",""params"":{""nolog"":true,""id"":""" + url + @""",""source"":""widget"",""userId"":""@viewer"",""groupId"":""@self""},""jsonrpc"":""2.0"",""key"":""p"",""apiVersion"":""v1""}]";
System.Net.HttpWebRequest request = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(googleApiUrl);
request.Method = "POST";
request.ContentType = "application/json-rpc";
request.ContentLength = postData.Length;
System.IO.Stream writeStream = request.GetRequestStream();
UTF8Encoding encoding = new UTF8Encoding();
byte[] bytes = encoding.GetBytes(postData);
writeStream.Write(bytes, 0, bytes.Length);
writeStream.Close();
System.Net.HttpWebResponse response = (System.Net.HttpWebResponse)request.GetResponse();
System.IO.Stream responseStream = response.GetResponseStream();
System.IO.StreamReader readStream = new System.IO.StreamReader(responseStream, Encoding.UTF8);
string jsonString = readStream.ReadToEnd();
readStream.Close();
responseStream.Close();
response.Close();
var json = new System.Web.Script.Serialization.JavaScriptSerializer().Deserialize(jsonString);
int count = Int32.Parse(json[0]["result"]["metadata"]["globalCounts"]["count"].ToString().Replace(".0", ""));
return count;
}
但这其中有一个错误
var json = new System.Web.Script.Serialization.JavaScriptSerializer().Deserialize(jsonString);
因为反序列化
方法有两个参数,我不知道第二个参数。
有人知道第二个论点是什么,或者知道另一个很好的方法吗
更新
我也试过了
public static Regex REGEX_GETURLCOUNT =
new Regex(@"<div[^>]+id=""aggregateCount""[^>]+>(\d*)</div>");
public int GetPlusOnes(string url)
{
string fetchUrl =
"https://plusone.google.com/u/0/_/+1/fastbutton?url=" +
HttpUtility.UrlEncode(url) + "&count=true";
WebClient wc=new WebClient();
string response = wc.DownloadString(fetchUrl);
Match match = REGEX_GETURLCOUNT.Match(response);
if (match.Success)
{
return int.Parse(match.Groups[1].Value);
}
return 0;
}
公共静态正则表达式Regex\u GETURLCOUNT=
新的正则表达式(@“]+id=”“aggregateCount”“[^>]+>(\d*)”;
public int GetPlusOnes(字符串url)
{
字符串获取URL=
"https://plusone.google.com/u/0/_/+1/fastbutton?url=”+
HttpUtility.UrlEncode(url)+“&count=true”;
WebClient wc=新的WebClient();
字符串响应=wc.DownloadString(fetchUrl);
Match Match=REGEX_GETURLCOUNT.Match(响应);
如果(匹配成功)
{
返回int.Parse(match.Groups[1].Value);
}
返回0;
}
但是
match.Success
总是false
最近我不得不做类似的事情,我使用:
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
int test = GetPlusOnes("http://www.allfancydress.com/"); //Returns 11 plus ones
Response.Write("Plus ones: " + test.ToString());
}
public static Regex REGEX_GETURLCOUNT =
new Regex(@"<div[^>]+id=""aggregateCount""[^>]+>(\d*)</div>");
public static int GetPlusOnes(string url)
{
string fetchUrl =
"https://plusone.google.com/u/0/_/+1/fastbutton?url=" +
HttpUtility.UrlEncode(url) + "&count=true";
HttpWebRequest request =
(HttpWebRequest)WebRequest.Create(fetchUrl);
string response = new StreamReader(request.GetResponse()
.GetResponseStream()).ReadToEnd();
Match match = REGEX_GETURLCOUNT.Match(response);
if (match.Success)
{
return int.Parse(match.Groups[1].Value);
}
return 0;
}
}
public分部类\u默认值:System.Web.UI.Page
{
受保护的无效页面加载(对象发送方、事件参数e)
{
int test=GetPlusOnes(“http://www.allfancydress.com/“”;//返回11加1
Response.Write(“加1:+test.ToString());
}
公共静态正则表达式Regex\u GETURLCOUNT=
新的正则表达式(@“]+id=”“aggregateCount”“[^>]+>(\d*)”;
公共静态int GetPlusOnes(字符串url)
{
字符串获取URL=
"https://plusone.google.com/u/0/_/+1/fastbutton?url=”+
HttpUtility.UrlEncode(url)+“&count=true”;
HttpWebRequest请求=
(HttpWebRequest)WebRequest.Create(fetchUrl);
string response=newstreamreader(request.GetResponse()
.GetResponseStream()).ReadToEnd();
Match Match=REGEX_GETURLCOUNT.Match(响应);
如果(匹配成功)
{
返回int.Parse(match.Groups[1].Value);
}
返回0;
}
}
希望这有帮助
*编辑了我的自定义代理类,您只需剪切并粘贴它,它就可以为您工作。我使用System.Web进行了
但是那里没有任何HttpUtility
!这就是它所在的位置,请参阅:第行中的gp
是什么:string response=gp.Request(fetchUrl)代码>?“gp”是我创建的一个类,用于生成新的代理,“Request”方法获取一个url,然后在代理后面执行其请求。显然,您需要稍微修改一下这段代码,但您需要的一切都在那里。好的。但是在代码if(match.Success)
中总是返回false
!