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 }
相关文章推荐
- TQ2440 学习笔记—— 2、Win7PC 与虚拟机Ubuntu互通 ping 的网络设置
- 虚拟机中如何进行正确的网络配置
- 网络篇----解析xml
- TCP/IP体系结构及协议
- UNIX网络编程卷一:第十四章 高级I/O
- HttpClient/ HttpUrlConnection/UrlConnection
- SM32W108无线射频模块多个节点之间通信实例
- HTTP Debugger Pro v6.4 + Keygen For HTTP Debugger Pro v6.x
- 使用charles proxy for Mac来抓取手机App的网络包
- centos 设置网络 /etc/sysconfig/network,/etc/sysconfig/network-scripts/ifcfg-eth0
- 使用NSURLConnection发送http网络请求
- 真有超级IP?亿万级泡沫谁来接盘
- HttpUrlConnection和HttpClient的使用(doGet( )、doPost( )方法)
- 打造安全的App!iOS安全系列之 HTTPS
- 打造安全的App!iOS安全系列之 HTTPS
- 无线网络
- C++ 简单实现HTTP GET/POST 请求
- HTTP协议浅析 (2013-08-12 19:48:14) http://blog.sina.com.cn/s/blog_987d7bdd0101nxtu.html
- HDU 4279 Number(2012天津网络游戏---数论分析题)
- C#网络编程(接收文件) - Part.5