温馨提示:代码在线浏览功能只能做为源码浏览参考,如果想更进一步了解该代码请下载:LiveBlog v1.0测试版源码
当前文件:
LiveBlog/LiveBlog.Core/API/MetaWeblog/XMLRPCRequest.cs[15K,2009-6-12 11:47:02],打开代码结构图
LiveBlog/LiveBlog.Core/API/MetaWeblog/XMLRPCRequest.cs[15K,2009-6-12 11:47:02],打开代码结构图1using System; 2
using System.Collections.Generic; 3
using System.Globalization; 4
using System.IO; 5
using System.Text; 6
using System.Web; 7
using System.Xml; 8
9
namespace LiveBlog.Core.API.MetaWeblog 10
...{ 11
/**//// <summary> 12
/// Obejct is the incoming XML-RPC Request. Handles parsing the XML-RPC and 13
/// fills its properties with the values sent in the request. 14
/// </summary> 15
internal class XMLRPCRequest 16
...{ 17
Contructors#region Contructors 18
/**//// <summary> 19
/// Loads XMLRPCRequest object from HttpContext 20
/// </summary> 21
/// <param name="input">incoming HttpContext</param> 22
public XMLRPCRequest(HttpContext input) 23
...{ 24
string inputXML = ParseRequest(input); 25
LoadXMLRequest(inputXML); // Loads Method Call and Associated Variables 26
} 27
#endregion 28
29
Local Vars#region Local Vars 30
31
private string _methodName; 32
private List<XmlNode> _inputParams; 33
private string _appKey; 34
private string _blogID; 35
private int _numberOfPosts; 36
private string _password; 37
private MWAPost _post; 38
private string _postID; 39
private bool _publish; 40
private string _userName; 41
private MWAMediaObject _media; 42
private MWAPage _page; 43
private string _pageID; 44
45
#endregion 46
47
Public Properties#region Public Properties 48
/**//// <summary> 49
/// Name of Called Metaweblog Function 50
/// </summary> 51
public string MethodName 52
...{ 53
get ...{ return _methodName; } 54
} 55
56
/**//// <summary> 57
/// AppKey is a key generated by the calling application. It is sent with blogger API calls. 58
/// </summary> 59
/// <remarks> 60
/// BlogEngine.NET doesn't require specific AppKeys for API calls. It is no longer standard practive. 61
/// </remarks> 62
public string AppKey 63
...{ 64
get ...{ return _appKey; } 65
} 66
67
/**//// <summary> 68
/// ID of the Blog to call the function on. Since BlogEngine supports only a single blog instance, 69
/// this incoming parameter is not used. 70
/// </summary> 71
public string BlogID 72
...{ 73
get ...{ return _blogID; } 74
} 75
76
/**//// <summary> 77
/// MediaObject is a struct sent by the metaWeblog.newMediaObject function. 78
/// It contains information about the media and the object in a bit array. 79
/// </summary> 80
public MWAMediaObject MediaObject 81
...{ 82
get ...{ return _media; } 83
} 84
85
/**//// <summary> 86
/// Number of post request by the metaWeblog.getRecentPosts function 87
/// </summary> 88
public int NumberOfPosts 89
...{ 90
get ...{ return _numberOfPosts; } 91
} 92
93
/**//// <summary> 94
/// Password for user validation 95
/// </summary> 96
public string Password 97
...{ 98
get ...{ return _password; } 99
} 100
101
/**//// <summary> 102
/// Metaweblog Post struct containing information post including title, content, and categories. 103
/// </summary> 104
public MWAPost Post 105
...{ 106
get ...{ return _post; } 107
} 108
109
/**//// <summary> 110
/// Metaweblog Page Struct 111
/// </summary> 112
public MWAPage Page 113
...{ 114
get ...{ return _page; } 115
} 116
117
/**//// <summary> 118
/// The PostID Guid in string format 119
/// </summary> 120
public string PostID 121
...{ 122
get ...{ return _postID; } 123
} 124
125
/**//// <summary> 126
/// PageID Guid in string format 127
/// </summary> 128
public string PageID 129
...{ 130
get ...{ return _pageID; } 131
} 132
133
/**//// <summary> 134
/// Publish determines wheter or not a post will be marked as published by BlogEngine. 135
/// </summary> 136
public bool Publish 137
...{ 138
get ...{ return _publish; } 139
} 140
141
/**//// <summary> 142
/// Login for user validation 143
/// </summary> 144
public string UserName 145
...{ 146
get ...{ return _userName; } 147
} 148
149
#endregion 150
151
Private Methods#region Private Methods 152
153
/**//// <summary> 154
/// Retrieves the content of the input stream 155
/// and return it as plain text. 156
/// </summary> 157
private string ParseRequest(HttpContext context) 158
...{ 159
byte[] buffer = new byte[context.Request.InputStream.Length]; 160
context.Request.InputStream.Read(buffer, 0, buffer.Length); 161
162
return System.Text.Encoding.UTF8.GetString(buffer); 163
} 164
165
/**//// <summary> 166
/// Loads object properties with contents of passed xml 167
/// </summary> 168
/// <param name="xml">xml doc with methodname and parameters</param> 169
private void LoadXMLRequest(string xml) 170
...{ 171
XmlDocument request = new XmlDocument(); 172
try 173
...{ 174
if (!(xml.StartsWith("<?xml") || xml.StartsWith("<method"))) 175
...{ 176
xml = xml.Substring(xml.IndexOf("<?xml")); 177
} 178
request.LoadXml(xml); 179
} 180
catch (Exception ex) 181
...{ 182
throw new MetaWeblogException("01", "Invalid XMLRPC Request. (" + ex.Message + ")"); 183
} 184
185
// Method name is always first 186
_methodName = request.DocumentElement.ChildNodes[0].InnerText; 187
188
// Parameters are next (and last) 189
_inputParams = new List<XmlNode>(); 190
foreach (XmlNode node in request.SelectNodes("/methodCall/params/param")) 191
...{ 192
_inputParams.Add(node); 193
} 194
195
// Determine what params are what by method name 196
switch (_methodName) 197
...{ 198
case "metaWeblog.newPost": 199
_blogID = _inputParams[0].InnerText; 200
_userName = _inputParams[1].InnerText; 201
_password = _inputParams[2].InnerText; 202
_post = GetPost(_inputParams[3]); 203
if (_inputParams[4].InnerText == "0" || _inputParams[4].InnerText == "false") 204
_publish = false; 205
else 206
_publish = true; 207
break; 208
case "metaWeblog.editPost": 209
_postID = _inputParams[0].InnerText; 210
_userName = _inputParams[1].InnerText; 211
_password = _inputParams[2].InnerText; 212
_post = GetPost(_inputParams[3]); 213
if (_inputParams[4].InnerText == "0" || _inputParams[4].InnerText == "false") 214
_publish = false; 215
else 216
_publish = true; 217
break; 218
case "metaWeblog.getPost": 219
_postID = _inputParams[0].InnerText; 220
_userName = _inputParams[1].InnerText; 221
_password = _inputParams[2].InnerText; 222
break; 223
case "metaWeblog.newMediaObject": 224
_blogID = _inputParams[0].InnerText; 225
_userName = _inputParams[1].InnerText; 226
_password = _inputParams[2].InnerText; 227
_media = GetMediaObject(_inputParams[3]); 228
break; 229
case "metaWeblog.getCategories": 230
case "wp.getPageList": 231
case "wp.getPages": 232
_blogID = _inputParams[0].InnerText; 233
_userName = _inputParams[1].InnerText; 234
_password = _inputParams[2].InnerText; 235
break; 236
case "metaWeblog.getRecentPosts": 237
_blogID = _inputParams[0].InnerText; 238
_userName = _inputParams[1].InnerText; 239
_password = _inputParams[2].InnerText; 240
_numberOfPosts = Int32.Parse(_inputParams[3].InnerText, CultureInfo.InvariantCulture); 241
break; 242
case "blogger.getUsersBlogs": 243
case "metaWeblog.getUsersBlogs": 244
_appKey = _inputParams[0].InnerText; 245
_userName = _inputParams[1].InnerText; 246
_password = _inputParams[2].InnerText; 247
break; 248
case "blogger.deletePost": 249
_appKey = _inputParams[0].InnerText; 250
_postID = _inputParams[1].InnerText; 251
_userName = _inputParams[2].InnerText; 252
_password = _inputParams[3].InnerText; 253
if (_inputParams[4].InnerText == "0" || _inputParams[4].InnerText == "false") 254
_publish = false; 255
else 256
_publish = true; 257
break; 258
case "blogger.getUserInfo": 259
_appKey = _inputParams[0].InnerText; 260
_userName = _inputParams[1].InnerText; 261
_password = _inputParams[2].InnerText; 262
break; 263
case "wp.newPage": 264
_blogID = _inputParams[0].InnerText; 265
_userName = _inputParams[1].InnerText; 266
_password = _inputParams[2].InnerText; 267
_page = GetPage(_inputParams[3]); 268
if (_inputParams[4].InnerText == "0" || _inputParams[4].InnerText == "false") 269
_publish = false; 270
else 271
_publish = true; 272
break; 273
case "wp.getPage": 274
_blogID = _inputParams[0].InnerText; 275
_pageID = _inputParams[1].InnerText; 276
_userName = _inputParams[2].InnerText; 277
_password = _inputParams[3].InnerText; 278
break; 279
case "wp.editPage": 280
_blogID = _inputParams[0].InnerText; 281
_pageID = _inputParams[1].InnerText; 282
_userName = _inputParams[2].InnerText; 283
_password = _inputParams[3].InnerText; 284
_page = GetPage(_inputParams[4]); 285
if (_inputParams[5].InnerText == "0" || _inputParams[5].InnerText == "false") 286
_publish = false; 287
else 288
_publish = true; 289
break; 290
case "wp.deletePage": 291
_blogID = _inputParams[0].InnerText; 292
_userName = _inputParams[1].InnerText; 293
_password = _inputParams[2].InnerText; 294
_pageID = _inputParams[3].InnerText; 295
break; 296
default: 297
throw new MetaWeblogException("02", "Unknown Method. (" + _methodName + ")"); 298
299
} 300
301
} 302
303
/**//// <summary> 304
/// Creates a Metaweblog Post object from the XML struct 305
/// </summary> 306
/// <param name="node">XML contains a Metaweblog Post Struct</param> 307
/// <returns>Metaweblog Post Struct Obejct</returns> 308
private MWAPost GetPost(XmlNode node) 309
...{ 310
MWAPost temp = new MWAPost(); 311
List<string> cats = new List<string>(); 312
List<string> tags = new List<string>(); 313
314
// Require Title and Description 315
try 316
...{ 317
temp.title = node.SelectSingleNode("value/struct/member[name='title']").LastChild.InnerText; 318
temp.description = node.SelectSingleNode("value/struct/member[name='description']").LastChild.InnerText; 319
} 320
catch (Exception ex) 321
...{ 322
throw new MetaWeblogException("05", "Post Struct Element, Title or Description, not Sent. (" + ex.Message + ")"); 323
} 324
if (node.SelectSingleNode("value/struct/member[name='link']") == null) 325
temp.link = ""; 326
else 327
temp.link = node.SelectSingleNode("value/struct/member[name='link']").LastChild.InnerText; 328
329
if (node.SelectSingleNode("value/struct/member[name='mt_allow_comments']") == null) 330
temp.commentPolicy = ""; 331
else 332
temp.commentPolicy = node.SelectSingleNode("value/struct/member[name='mt_allow_comments']").LastChild.InnerText; 333
334
if (node.SelectSingleNode("value/struct/member[name='mt_excerpt']") == null) 335
temp.excerpt = ""; 336
else 337
temp.excerpt = node.SelectSingleNode("value/struct/member[name='mt_excerpt']").LastChild.InnerText; 338
339
if (node.SelectSingleNode("value/struct/member[name='wp_slug']") == null) 340
temp.slug = ""; 341
else 342
temp.slug = node.SelectSingleNode("value/struct/member[name='wp_slug']").LastChild.InnerText; 343
344
if (node.SelectSingleNode("value/struct/member[name='categories']") != null) 345
...{ 346
XmlNode categoryArray = node.SelectSingleNode("value/struct/member[name='categories']").LastChild; 347
foreach (XmlNode catnode in categoryArray.SelectNodes("array/data/value/string")) 348
...{ 349
cats.Add(catnode.InnerText); 350
} 351
} 352
temp.categories = cats; 353
354
if (node.SelectSingleNode("value/struct/member[name='pubDate']") != null) 355
...{ 356
try 357
...{ 358
string tempDate = node.SelectSingleNode("value/struct/member[name='pubDate']").LastChild.InnerText; 359
temp.postDate = DateTime.ParseExact(tempDate, "yyyyMMdd'T'HH':'mm':'ss", CultureInfo.InvariantCulture, DateTimeStyles.AssumeUniversal); 360
} 361
catch 362
...{ 363
// Ignore PubDate Error 364
} 365
} 366
367
// WLW tags implementation using mt_keywords 368
if (node.SelectSingleNode("value/struct/member[name='mt_keywords']") != null) 369
...{ 370
string tagsList = node.SelectSingleNode("value/struct/member[name='mt_keywords']").LastChild.InnerText; 371
foreach (string item in tagsList.Split(',')) 372
...{ 373
tags.Add(item.Trim().ToLowerInvariant()); 374
} 375
} 376
temp.tags = tags; 377
378
return temp; 379
} 380
381
/**//// <summary> 382
/// Creates a Metaweblog Page object from the XML struct 383
/// </summary> 384
/// <param name="node">XML contains a Metaweblog Page Struct</param> 385
/// <returns>Metaweblog Page Struct Obejct</returns> 386
private MWAPage GetPage(XmlNode node) 387
...{ 388
MWAPage temp = new MWAPage(); 389
390
// Require Title and Description 391
try 392
...{ 393
temp.title = node.SelectSingleNode("value/struct/member[name='title']").LastChild.InnerText; 394
temp.description = node.SelectSingleNode("value/struct/member[name='description']").LastChild.InnerText; 395
} 396
catch (Exception ex) 397
...{ 398
throw new MetaWeblogException("06", "Page Struct Element, Title or Description, not Sent. (" + ex.Message + ")"); 399
} 400
if (node.SelectSingleNode("value/struct/member[name='link']") == null) 401
temp.link = ""; 402
else 403
temp.link = node.SelectSingleNode("value/struct/member[name='link']").LastChild.InnerText; 404
405
if (node.SelectSingleNode("value/struct/member[name='dateCreated']") != null) 406
...{ 407
try 408
...{ 409
string tempDate = node.SelectSingleNode("value/struct/member[name='dateCreated']").LastChild.InnerText; 410
temp.pageDate = DateTime.ParseExact(tempDate, "yyyyMMdd'T'HH':'mm':'ss", CultureInfo.InvariantCulture, DateTimeStyles.AssumeUniversal); 411
} 412
catch 413
...{ 414
// Ignore PubDate Error 415
} 416
} 417
418
if (node.SelectSingleNode("value/struct/member[name='wp_page_parent_id']") != null) 419
temp.pageParentID = node.SelectSingleNode("value/struct/member[name='wp_page_parent_id']").LastChild.InnerText; 420
421
return temp; 422
} 423
424
/**//// <summary> 425
/// Creates a Metaweblog Media object from the XML struct 426
/// </summary> 427
/// <param name="node">XML contains a Metaweblog MediaObject Struct</param> 428
/// <returns>Metaweblog MediaObject Struct Obejct</returns> 429
private MWAMediaObject GetMediaObject(XmlNode node) 430
...{ 431
MWAMediaObject temp = new MWAMediaObject(); 432
temp.name = node.SelectSingleNode("value/struct/member[name='name']").LastChild.InnerText; 433
if (node.SelectSingleNode("value/struct/member[name='type']") == null) 434
temp.type = "notsent"; 435
else 436
temp.type = node.SelectSingleNode("value/struct/member[name='type']").LastChild.InnerText; 437
temp.bits = System.Convert.FromBase64String(node.SelectSingleNode("value/struct/member[name='bits']").LastChild.InnerText); 438
439
return temp; 440
} 441
442
#endregion 443
444
} 445
} 446






}