您的位置:首页 > 理论基础 > 计算机网络

BIM360: C#如何发送HTTP GET和POST请求登陆BIM 360 Glue以及获取项目列表

2015-08-12 10:29 1051 查看

登陆

首先是登陆,通过发送HTTP POST请求:

int timestamp = GetTimestamp();
string sig = GetSig(api_key, api_secret, timestamp);
string url = "https://b4.autodesk.com/api/security/v1/login.json";
HttpWebRequest oRequest = (HttpWebRequest)HttpWebRequest.Create(url);
oRequest.Method = "POST";
var postData =
"pretty=1&" +
"login_name=" + username +
"&password=" + password +
"&company_id=" + company_id +
"&api_key=" + api_key +
"&api_secret=" + api_secret +
"×tamp=" + timestamp +
"&sig=" + sig;

textBoxRequest.Text = postData;

var data = Encoding.ASCII.GetBytes(postData);
oRequest.ContentType = "application/x-www-form-urlencoded";
oRequest.ContentLength = data.Length;

using (var stream = oRequest.GetRequestStream())
{
stream.Write(data, 0, data.Length);
}

HttpWebResponse oResponse = (HttpWebResponse)oRequest.GetResponse();
string tBody = new StreamReader(oResponse.GetResponseStream()).ReadToEnd();

代码中HttpWebRequest的命名空间是System.Net

pretty=1参数会格式化返回的JSON,方便查看。

获取timestamp和sig的方法如下:

private int GetTimestamp()
{
TimeSpan tSpan = (DateTime.UtcNow - new DateTime(1970, 1, 1));
int timestamp = (int)tSpan.TotalSeconds;
return timestamp;
}

private string GetSig(string apikey, string apisecret, int timestamp)
{
return ComputeMD5Hash(apikey + apisecret + timestamp);
}


登陆之后,返回的结果如下:

{
"auth_token": "6df75b2959704f86b797fd4b5cbec6af",
"user_id": "2274c83b-79fd-46ff-8772-38726b348c7f",
"account_id": "1074a18f-91bf-4b8a-99ed-8d7625958619",
"account_hostname": "adn"
}


获取项目列表

获取项目列表需要用GET方法,关键点在于如何构建请求的网址:
int timestamp = GetTimestamp();
string sig = GetSig(api_key, api_secret, timestamp);
string url = "https://b4.autodesk.com/api/project/v1/list.json";
var urlParameters =
"pretty=1&" +
"company_id=" + company_id +
"&api_key=" + api_key +
"×tamp=" + timestamp +
"&auth_token=" + auth_token +
"&sig=" + sig;

url += "?" + urlParameters;

HttpWebRequest oRequest = (HttpWebRequest)HttpWebRequest.Create(url);
oRequest.Method = "GET";

HttpWebResponse oResponse = (HttpWebResponse)oRequest.GetResponse();
string tBody = new StreamReader(oResponse.GetResponseStream()).ReadToEnd();


返回结果:
{
"project_list": [
{
"recent_model_info": [],
"project_id": "1e7e5e8d-fea8-49cd-8e5b-76058f0ee3b6",
"project_name": "AL+Sample+Project",
"company_id": "adn",
"created_date": "2015-03-19 03:26:51",
"modify_date": "2015-05-06 14:45:40",
"cmic_company_code": "",
"cmic_project_code": "",
"cw_project_code": "",
"thumbnail_modified_date": "2015-03-19 04:41:45",
"has_views": false,
"has_markups": false,
"has_clashes": false,
"has_points": false,
"total_member_count": 0,
"total_project_admin_count": 0,
"total_views_count": 0,
"total_markups_count": 0,
"last_activity_date": "2015-05-06 14:45:40",
"permissions": [],
"navisworks_version": "Nwd2014"
}
],
"page": 1,
"page_size": 1,
"total_result_size": 1,
"more_pages": 0
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: