forked from thinkry/wecode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathXMLAPI.cs
410 lines (327 loc) · 14.7 KB
/
XMLAPI.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration;
using System.Net;
using System.IO;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json;
using System.Xml;
using System.Web;
using System.Collections.Specialized;
using System.Data.OleDb;
namespace WeCode1._0
{
public static class XMLAPI
{
/// <summary>
/// 本地XML同步到有道云笔记上
/// </summary>
public static void XML2Yun()
{
string sPath = "";
try
{
//获取AccessTokon构造URL
StringBuilder url = new StringBuilder("https://note.youdao.com/yws/open/notebook/list.json"); //可变字符串
//获取access_token构造POST参数
StringBuilder urlPoststr = new StringBuilder(""); // 可变字符串
//追加组合格式字符串
urlPoststr.AppendFormat("oauth_token={0}&", ConfigurationManager.AppSettings["AccessToken"]);
urlPoststr.AppendFormat("¬ebook={0}&", getWecodeDirPath());
//获取POST提交数据返回内容
string JNotebookListAll = sendMessage(url.ToString(), urlPoststr.ToString());
//获取的数据装换为Json格式 此时返回的json格式的数据
JArray ja = (JArray)JsonConvert.DeserializeObject(JNotebookListAll);
//默认只有一篇笔记,因此直接返回路径
JValue jv = (JValue)ja[0];
sPath = jv.ToString();
XmlDocument doc = new XmlDocument();
doc.Load("TreeNodeLocal.xml");
string content = doc.InnerXml;
UpdateXML(sPath, content);
}
catch (Exception msg) //异常处理
{
}
}
//修改XML
/// <summary>
///
/// </summary>
/// <param name="path"></param>
/// <param name="content">XML</param>
public static void UpdateXML(string path, string content)
{
if (ConfigurationManager.AppSettings["AccessToken"] != "")
{
try
{
//编码
string eContent = HttpUtility.HtmlEncode(content);
//获取AccessTokon构造URL
StringBuilder url = new StringBuilder("https://note.youdao.com/yws/open/note/update.json"); //可变字符串
NameValueCollection myCol = new NameValueCollection();
myCol.Add("path", path);//如果键值red相同结果合并 rojo,rouge
myCol.Add("content", eContent);
string aaa = HttpPostData(url.ToString(), myCol);
}
catch (Exception msg) //异常处理
{
}
}
}
/// <summary>
/// 获取远端XML
/// </summary>
/// <param name="path">笔记路径</param>
/// <returns>html解码之后的文本</returns>
public static string GetXML(string path)
{
string result = "";
if (ConfigurationManager.AppSettings["AccessToken"] != "")
{
try
{
//获取AccessTokon构造URL
StringBuilder url = new StringBuilder("https://note.youdao.com/yws/open/note/get.json"); //可变字符串
//获取access_token构造POST参数
StringBuilder urlPoststr = new StringBuilder(""); // 可变字符串
//追加组合格式字符串
urlPoststr.AppendFormat("oauth_token={0}&", ConfigurationManager.AppSettings["AccessToken"]);
urlPoststr.AppendFormat("&path={0}&", path);
//获取POST提交数据返回内容
string JNotebookListAll = sendMessage(url.ToString(), urlPoststr.ToString());
//获取的数据装换为Json格式 此时返回的json格式的数据
JObject o = JObject.Parse(JNotebookListAll);
string oa = o["content"].ToString();
result = HttpUtility.HtmlDecode(oa);
}
catch (Exception msg) //异常处理
{
}
}
return result;
}
/// <summary>
/// 获取笔记的content,转换成root为节点的XML
/// </summary>
/// <param name="path">笔记路径</param>
/// <returns>xmlDocument对象</returns>
public static XmlDocument GetNoteWithXML(string path)
{
XmlDocument xDoc = new XmlDocument();
if (ConfigurationManager.AppSettings["AccessToken"] != "")
{
try
{
//获取AccessTokon构造URL
StringBuilder url = new StringBuilder("https://note.youdao.com/yws/open/note/get.json"); //可变字符串
//获取access_token构造POST参数
StringBuilder urlPoststr = new StringBuilder(""); // 可变字符串
//追加组合格式字符串
urlPoststr.AppendFormat("oauth_token={0}&", ConfigurationManager.AppSettings["AccessToken"]);
urlPoststr.AppendFormat("&path={0}&", path);
//获取POST提交数据返回内容
string JNotebookListAll = sendMessage(url.ToString(), urlPoststr.ToString());
//获取的数据装换为Json格式 此时返回的json格式的数据
JObject o = JObject.Parse(JNotebookListAll);
string oa = o["content"].ToString();
xDoc.LoadXml("<root>" + oa + "</root>");
}
catch (Exception msg) //异常处理
{
}
}
return xDoc;
}
/// <summary>
/// 有道云笔记的XML同步到本地XML
/// </summary>
public static void Yun2XML()
{
string result = "";
string sPath = "";
//先列出所有笔记
try
{
//获取AccessTokon构造URL
StringBuilder url = new StringBuilder("https://note.youdao.com/yws/open/notebook/list.json"); //可变字符串
//获取access_token构造POST参数
StringBuilder urlPoststr = new StringBuilder(""); // 可变字符串
//追加组合格式字符串
urlPoststr.AppendFormat("oauth_token={0}&", ConfigurationManager.AppSettings["AccessToken"]);
urlPoststr.AppendFormat("¬ebook={0}&", getWecodeDirPath());
//获取POST提交数据返回内容
string JNotebookListAll = sendMessage(url.ToString(), urlPoststr.ToString());
//获取的数据装换为Json格式 此时返回的json格式的数据
JArray ja = (JArray)JsonConvert.DeserializeObject(JNotebookListAll);
//默认只有一篇笔记,因此直接返回路径
JValue jv = (JValue)ja[0];
sPath = jv.ToString();
string XMLContent = GetXML(sPath);
result = XMLContent;
//写入到本地
XmlDocument doc = new XmlDocument();
doc.LoadXml(result);
doc.Save("TreeNodeLocal.xml");
//拉取完了检查升级
CheckDb.CheckUpdateXML();
//生成目录数据
LocalXml2TTree();
}
catch (Exception msg) //异常处理
{
result = "";
}
}
/// <summary>
/// 拉取云目录之后重新更新youdao数据库的目录,同时重新更新文章表与附件表
/// 并且同住主界面开始扫描线程进行同步
/// </summary>
public static void LocalXml2TTree()
{
XML2DB xml2tree = new XML2DB();
xml2tree.MakeXML2Tttree();
//更新文章表和附件表
updateAttTcon();
//开始扫描线程
Attachment.frmMain.BeginSyncThread();
}
/// <summary>
/// 根据gid来重新更新nodeid信息,使得缓存数据库可以直接供本地打开
/// </summary>
public static void updateAttTcon()
{
OleDbConnection ExportConn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+PubFunc.GetYoudaoDBPath());
string SQL = "update tcontent set nodeid=-2";
AccessAdo.ExecuteNonQuery(ExportConn, SQL);
SQL = "update tcontent inner join ttree on tcontent.gid=ttree.gid set tcontent.nodeid=ttree.nodeid";
AccessAdo.ExecuteNonQuery(ExportConn, SQL);
SQL = "update tattachment inner join ttree on tattachment.gid=ttree.gid set tattachment.nodeid=ttree.nodeid";
AccessAdo.ExecuteNonQuery(ExportConn, SQL);
}
//获取wecode目录路径(仅存放目录配置一篇笔记)
public static string getWecodeDirPath()
{
string result = "";
//wecode笔记本路径
try
{
//获取AccessTokon构造URL
StringBuilder url = new StringBuilder("https://note.youdao.com/yws/open/notebook/all.json"); //可变字符串
//获取access_token构造POST参数
StringBuilder urlPoststr = new StringBuilder(""); // 可变字符串
//追加组合格式字符串
urlPoststr.AppendFormat("oauth_token={0}&", ConfigurationManager.AppSettings["AccessToken"]);
//获取POST提交数据返回内容
string JNotebookListAll = sendMessage(url.ToString(), urlPoststr.ToString());
//获取的数据装换为Json格式 此时返回的json格式的数据
//JObject obj = JObject.Parse(AccessContent);
JArray ja = (JArray)JsonConvert.DeserializeObject(JNotebookListAll);
foreach (JObject jo in ja)
{
if (jo["name"].ToString() == "wecode")
{
result = jo["path"].ToString();
}
}
}
catch (Exception msg) //异常处理
{
result = "";
}
return result;
}
//发送消息Post方法
public static string sendMessage(string strUrl, string PostStr)
{
try
{
//设置消息头
CookieContainer objCookieContainer = null;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(strUrl);
request.Method = "Post";
request.ContentType = "application/x-www-form-urlencoded";
request.Referer = strUrl;
if (objCookieContainer == null)
objCookieContainer = new CookieContainer();
request.CookieContainer = objCookieContainer;
byte[] byteData = Encoding.UTF8.GetBytes(PostStr.ToString().TrimEnd('&'));
request.ContentLength = byteData.Length;
using (Stream reqStream = request.GetRequestStream())
{
reqStream.Write(byteData, 0, byteData.Length);
}
//Response应答流获取数据
string strResponse = "";
using (HttpWebResponse res = (HttpWebResponse)request.GetResponse())
{
objCookieContainer = request.CookieContainer;
using (Stream resStream = res.GetResponseStream())
{
using (StreamReader sr = new StreamReader(resStream, Encoding.UTF8)) //UTF8格式
{
strResponse = sr.ReadToEnd();
}
}
// res.Close();
}
return strResponse;
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
Console.Read();
}
return null;
}
//post数据,ContentType = "multipart/form-data"
private static string HttpPostData(string url, NameValueCollection stringDict)
{
//字符串参数
string Pararesult = "";
string responseContent;
var memStream = new MemoryStream();
var webRequest = (HttpWebRequest)WebRequest.Create(url);
// 边界符
var boundary = "---------------" + DateTime.Now.Ticks.ToString("x");
// 边界符
var beginBoundary = Encoding.ASCII.GetBytes("--" + boundary + "\r\n");
// 最后的结束符
var endBoundary = Encoding.ASCII.GetBytes("--" + boundary + "--\r\n");
// 设置属性
webRequest.Method = "POST";
webRequest.Headers.Add("Authorization", "OAuth oauth_token=\"" + ConfigurationManager.AppSettings["AccessToken"] + "\"");
webRequest.ContentType = "multipart/form-data; boundary=" + boundary;
//写入字符串的Key
string stringKeyHeader = "--" + boundary + "\r\n" + "Content-Disposition: form-data; name=\"{0}\"" +
"\r\n\r\n{1}\r\n";
foreach (string key in stringDict.Keys)
{
Pararesult += string.Format(stringKeyHeader, key, stringDict[key]);
}
var paraByte = Encoding.UTF8.GetBytes(Pararesult);
memStream.Write(paraByte, 0, paraByte.Length);
// 写入最后的结束边界符
memStream.Write(endBoundary, 0, endBoundary.Length);
webRequest.ContentLength = memStream.Length;
var requestStream = webRequest.GetRequestStream();
memStream.Position = 0;
var tempBuffer = new byte[memStream.Length];
memStream.Read(tempBuffer, 0, tempBuffer.Length);
memStream.Close();
requestStream.Write(tempBuffer, 0, tempBuffer.Length);
requestStream.Close();
var httpWebResponse = (HttpWebResponse)webRequest.GetResponse();
using (var httpStreamReader = new StreamReader(httpWebResponse.GetResponseStream(),
Encoding.GetEncoding("utf-8")))
{
responseContent = httpStreamReader.ReadToEnd();
}
httpWebResponse.Close();
webRequest.Abort();
return responseContent;
}
}
}