我对c#还是新手,我正在try 为这个页面创建一个应用程序,当我收到通知(回复、 comments 等)时,它会告诉我.但现在我只是try 对api进行一个简单的调用,它将获取用户的数据.
我正在使用Visual studio Express 2012构建C#应用程序,在其中(目前)输入您的用户id,因此应用程序将使用用户id发出请求并显示此用户id的统计信息.
下面是我试图提出请求的代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
//Request library
using System.Net;
using System.IO;
namespace TestApplication
{
class Connect
{
public string id;
public string type;
protected string api = "https://api.stackexchange.com/2.2/";
protected string options = "?order=desc&sort=name&site=stackoverflow";
public string request()
{
string totalUrl = this.join(id);
return this.HttpGet(totalUrl);
}
protected string join(string s)
{
return api + type + "/" + s + options;
}
protected string get(string url)
{
try
{
string rt;
WebRequest request = WebRequest.Create(url);
WebResponse response = request.GetResponse();
Stream dataStream = response.GetResponseStream();
StreamReader reader = new StreamReader(dataStream);
rt = reader.ReadToEnd();
Console.WriteLine(rt);
reader.Close();
response.Close();
return rt;
}
catch(Exception ex)
{
return "Error: " + ex.Message;
}
}
public string HttpGet(string URI)
{
WebClient client = new WebClient();
// Add a user agent header in case the
// requested URI contains a query.
client.Headers.Add("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)");
Stream data = client.OpenRead(URI);
StreamReader reader = new StreamReader(data);
string s = reader.ReadToEnd();
data.Close();
reader.Close();
return s;
}
}
}
该类是一个对象,通过解析用户id并发出请求,就可以从表单中访问它.
我试过谷歌上的许多例子,但不知道为什么我能理解这条信息."�".
我是这种算法的新手,如果有人能分享一本书或教程,展示如何做这类事情(解释每一步),我将不胜感激