我正在尝试使用此article
获取谷歌加一个计数这是我的代码:
// Create a request using a URL that can receive a post.
WebRequest request = WebRequest.Create("https://clients6.google.com/rpc?key=AIzaSyAr1iwJ7rDsRx9BwS6M6MArYrNHfDtQJqk");
// Set the Method property of the request to POST.
request.Method = "POST";
request.ContentType = "application/json";
var paramss = new MyStruct()
{
id = domain,
source = "widget",
nolog = true,
userId = "@viewer",
groupId = "@self",
};
JavaScriptSerializer serializer = new JavaScriptSerializer();
var str = serializer.Serialize(paramss);
// Create POST data and convert it to a byte array.
string postData = "method=pos.plusones.get";
postData += "&id=p";
postData += "&jsonrpc=2.0";
postData += "&key=p";
postData += "&apiVersion=v1";
postData += "¶ms=" + str;
byte[] byteArray = Encoding.UTF8.GetBytes(postData);
// Set the ContentLength property of the WebRequest.
request.ContentLength = byteArray.Length;
// Get the request stream.
Stream dataStream = request.GetRequestStream();
// Write the data to the request stream.
dataStream.Write(byteArray, 0, byteArray.Length);
// Close the Stream object.
dataStream.Close();
// Get the response.
WebResponse response = request.GetResponse();
// Display the status.
Console.WriteLine(((HttpWebResponse)response).StatusDescription);
// Get the stream containing content returned by the server.
dataStream = response.GetResponseStream();
// Open the stream using a StreamReader for easy access.
StreamReader reader = new StreamReader(dataStream);
// Read the content.
string responseFromServer = reader.ReadToEnd();
但仍然遇到同样的错误:
{"error":{"code":-32700,"message":"Unable to parse json","data":[{"domain":"global","reason":"parseError","message":"Unable to parse json"}]}}
我不是要在我的代码上解析json,所以我真的不知道从哪里开始。 有什么想法吗?
答案 0 :(得分:0)
解决了它,遇到了以下article,这就是我所使用的:
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 JavaScriptSerializer().Deserialize<List<test>>(jsonString);
var dic = json.ToDictionary(d=>d.id, d=>d.result);
var count = Convert.ToInt32(decimal.Parse(((Dictionary<string, object>)((Dictionary<string, object>)((Dictionary<string, object>)dic["p"])["metadata"])["globalCounts"])["count"].ToString()));
return count;