OSDN Git Service

2336d4db76ba49d721dafd0ebc2b3a76242b2434
[opentween/open-tween.git] / Tween / Connection / HttpTwitter.vb
1 ' Tween - Client of Twitter
2 ' Copyright (c) 2007-2011 kiri_feather (@kiri_feather) <kiri.feather@gmail.com>
3 '           (c) 2008-2011 Moz (@syo68k)
4 '           (c) 2008-2011 takeshik (@takeshik) <http://www.takeshik.org/>
5 '           (c) 2010-2011 anis774 (@anis774) <http://d.hatena.ne.jp/anis774/>
6 '           (c) 2010-2011 fantasticswallow (@f_swallow) <http://twitter.com/f_swallow>
7 ' All rights reserved.
8
9 ' This file is part of Tween.
10
11 ' This program is free software; you can redistribute it and/or modify it
12 ' under the terms of the GNU General Public License as published by the Free
13 ' Software Foundation; either version 3 of the License, or (at your option)
14 ' any later version.
15
16 ' This program is distributed in the hope that it will be useful, but
17 ' WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
18 ' or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19 ' for more details. 
20
21 ' You should have received a copy of the GNU General Public License along
22 ' with this program. If not, see <http://www.gnu.org/licenses/>, or write to
23 ' the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
24 ' Boston, MA 02110-1301, USA.
25
26 Imports System.Net
27 Imports System.IO
28 Imports System.Web
29 Imports System.Threading
30
31 Public Class HttpTwitter
32     Implements ICloneable
33
34     'OAuth関連
35     '''<summary>
36     '''OAuthのコンシューマー鍵
37     '''</summary>
38     Private Const ConsumerKey As String = "tLbG3uS0BIIE8jm1mKzKOfZ6EgUOmWVM"
39
40     '''<summary>
41     '''OAuthの署名作成用秘密コンシューマーデータ
42     '''</summary>
43     Private Const ConsumerSecret As String = "M0IMsbl2722iWa+CGPVcNeQmE+TFpJk8B/KW9UUTk3eLOl9Ij005r52JNxVukTzM"
44     '''<summary>
45     '''OAuthのアクセストークン取得先URI
46     '''</summary>
47     Private Const AccessTokenUrlXAuth As String = "https://api.twitter.com/oauth/access_token"
48
49     Private Shared _protocol As String = "http://"
50
51     Private Const PostMethod As String = "POST"
52     Private Const GetMethod As String = "GET"
53
54     Private httpCon As IHttpConnection 'HttpConnectionApi or HttpConnectionOAuth
55     Private httpConVar As New HttpVarious
56
57     Private Enum AuthMethod
58         OAuth
59         Basic
60     End Enum
61     Private connectionType As AuthMethod = AuthMethod.Basic
62
63     Public Sub Initialize(ByVal accessToken As String, _
64                                     ByVal accessTokenSecret As String, _
65                                     ByVal username As String)
66         'for OAuth
67         Dim con As New HttpOAuthApiProxy
68         Static tk As String = ""
69         Static tks As String = ""
70         Static un As String = ""
71         If tk <> accessToken OrElse tks <> accessTokenSecret OrElse _
72                 un <> username OrElse connectionType <> AuthMethod.OAuth Then
73             ' 以前の認証状態よりひとつでも変化があったらhttpヘッダより読み取ったカウントは初期化
74             tk = accessToken
75             tks = accessTokenSecret
76             un = username
77         End If
78         con.Initialize(DecryptString(ConsumerKey), DecryptString(ConsumerSecret), accessToken, accessTokenSecret, username, "screen_name")
79         httpCon = con
80         connectionType = AuthMethod.OAuth
81     End Sub
82
83     Public Sub Initialize(ByVal username As String, _
84                                     ByVal password As String)
85         'for BASIC auth
86         Dim con As New HttpConnectionBasic
87         Static un As String = ""
88         Static pw As String = ""
89         If un <> username OrElse pw <> password OrElse connectionType <> AuthMethod.Basic Then
90             ' 以前の認証状態よりひとつでも変化があったらhttpヘッダより読み取ったカウントは初期化
91             un = username
92             pw = password
93         End If
94         con.Initialize(username, password)
95         httpCon = con
96         connectionType = AuthMethod.Basic
97     End Sub
98
99     Public ReadOnly Property AccessToken() As String
100         Get
101             If httpCon IsNot Nothing Then
102                 If connectionType = AuthMethod.Basic Then Return ""
103                 Return DirectCast(httpCon, HttpConnectionOAuth).AccessToken
104             Else
105                 Return ""
106             End If
107         End Get
108     End Property
109
110     Public ReadOnly Property AccessTokenSecret() As String
111         Get
112             If httpCon IsNot Nothing Then
113                 If connectionType = AuthMethod.Basic Then Return ""
114                 Return DirectCast(httpCon, HttpConnectionOAuth).AccessTokenSecret
115             Else
116                 Return ""
117             End If
118         End Get
119     End Property
120
121     Public ReadOnly Property AuthenticatedUsername() As String
122         Get
123             If httpCon IsNot Nothing Then
124                 Return httpCon.AuthUsername
125             Else
126                 Return ""
127             End If
128         End Get
129     End Property
130
131     Public ReadOnly Property Password() As String
132         Get
133             If httpCon IsNot Nothing Then
134                 'OAuthではパスワード取得させない
135                 If connectionType = AuthMethod.Basic Then Return DirectCast(httpCon, HttpConnectionBasic).Password
136                 Return ""
137             Else
138                 Return ""
139             End If
140         End Get
141     End Property
142
143     Public Function AuthUserAndPass(ByVal username As String, ByVal password As String, ByRef content As String) As HttpStatusCode
144         If connectionType = AuthMethod.Basic Then
145             Return httpCon.Authenticate(CreateTwitterUri("/1/account/verify_credentials.json"), username, password, content)
146         Else
147             Return httpCon.Authenticate(New Uri(AccessTokenUrlXAuth), username, password, content)
148         End If
149     End Function
150
151     Public Sub ClearAuthInfo()
152         If connectionType = AuthMethod.Basic Then
153             Me.Initialize("", "")
154         Else
155             Me.Initialize("", "", "")
156         End If
157     End Sub
158
159     Public Shared WriteOnly Property UseSsl() As Boolean
160         Set(ByVal value As Boolean)
161             If value Then
162                 _protocol = "https://"
163             Else
164                 _protocol = "http://"
165             End If
166         End Set
167     End Property
168
169     Public Function UpdateStatus(ByVal status As String, ByVal replyToId As Long, ByRef content As String) As HttpStatusCode
170         Dim param As New Dictionary(Of String, String)
171         param.Add("status", status)
172         If connectionType = AuthMethod.Basic Then param.Add("source", "Tween")
173         If replyToId > 0 Then param.Add("in_reply_to_status_id", replyToId.ToString)
174
175         Return httpCon.GetContent(PostMethod, _
176                             CreateTwitterUri("/1/statuses/update.json"), _
177                             param, _
178                             content, _
179                             Nothing, _
180                             Nothing)
181     End Function
182
183     Public Function DestroyStatus(ByVal id As Long) As HttpStatusCode
184         Return httpCon.GetContent(PostMethod, _
185                             CreateTwitterUri("/1/statuses/destroy/" + id.ToString + ".json"), _
186                             Nothing, _
187                             Nothing, _
188                             Nothing, _
189                             Nothing)
190     End Function
191
192     Public Function SendDirectMessage(ByVal status As String, ByVal sendto As String, ByRef content As String) As HttpStatusCode
193         Dim param As New Dictionary(Of String, String)
194         param.Add("text", status)
195         param.Add("screen_name", sendto)
196
197         Return httpCon.GetContent(PostMethod, _
198                             CreateTwitterUri("/1/direct_messages/new.json"), _
199                             param, _
200                             content, _
201                             Nothing, _
202                             Nothing)
203     End Function
204
205     Public Function DestroyDirectMessage(ByVal id As Long) As HttpStatusCode
206         Return httpCon.GetContent(PostMethod, _
207                             CreateTwitterUri("/1/direct_messages/destroy/" + id.ToString + ".json"), _
208                             Nothing, _
209                             Nothing, _
210                             Nothing, _
211                             Nothing)
212     End Function
213
214     Public Function RetweetStatus(ByVal id As Long, ByRef content As String) As HttpStatusCode
215         Return httpCon.GetContent(PostMethod, _
216                             CreateTwitterUri("/1/statuses/retweet/" + id.ToString() + ".json"), _
217                             Nothing, _
218                             content, _
219                             Nothing, _
220                             Nothing)
221     End Function
222
223     Public Function ShowUserInfo(ByVal screenName As String, ByRef content As String) As HttpStatusCode
224         Dim param As New Dictionary(Of String, String)
225         param.Add("screen_name", screenName)
226         Return httpCon.GetContent(GetMethod, _
227                             CreateTwitterUri("/1/users/show.json"), _
228                             param, _
229                             content, _
230                             TwitterApiInfo.HttpHeaders, _
231                             AddressOf GetApiCallback)
232     End Function
233
234     Public Function CreateFriendships(ByVal screenName As String, ByRef content As String) As HttpStatusCode
235         Dim param As New Dictionary(Of String, String)
236         param.Add("screen_name", screenName)
237
238         Return httpCon.GetContent(PostMethod, _
239                             CreateTwitterUri("/1/friendships/create.json"), _
240                             param, _
241                             content, _
242                             Nothing, _
243                             Nothing)
244     End Function
245
246     Public Function DestroyFriendships(ByVal screenName As String, ByRef content As String) As HttpStatusCode
247         Dim param As New Dictionary(Of String, String)
248         param.Add("screen_name", screenName)
249
250         Return httpCon.GetContent(PostMethod, _
251                             CreateTwitterUri("/1/friendships/destroy.json"), _
252                             param, _
253                             content, _
254                             Nothing, _
255                             Nothing)
256     End Function
257
258     Public Function CreateBlock(ByVal screenName As String, ByRef content As String) As HttpStatusCode
259         Dim param As New Dictionary(Of String, String)
260         param.Add("screen_name", screenName)
261
262         Return httpCon.GetContent(PostMethod, _
263                             CreateTwitterUri("/1/blocks/create.json"), _
264                             param, _
265                             content, _
266                             Nothing, _
267                             Nothing)
268     End Function
269
270     Public Function DestroyBlock(ByVal screenName As String, ByRef content As String) As HttpStatusCode
271         Dim param As New Dictionary(Of String, String)
272         param.Add("screen_name", screenName)
273
274         Return httpCon.GetContent(PostMethod, _
275                             CreateTwitterUri("/1/blocks/destroy.json"), _
276                             param, _
277                             content, _
278                             Nothing, _
279                             Nothing)
280     End Function
281
282     Public Function ReportSpam(ByVal screenName As String, ByRef content As String) As HttpStatusCode
283         Dim param As New Dictionary(Of String, String)
284         param.Add("screen_name", screenName)
285
286         Return httpCon.GetContent(PostMethod, _
287                             CreateTwitterUri("/1/report_spam.json"), _
288                             param, _
289                             content, _
290                             Nothing, _
291                             Nothing)
292     End Function
293
294     Public Function ShowFriendships(ByVal souceScreenName As String, ByVal targetScreenName As String, ByRef content As String) As HttpStatusCode
295         Dim param As New Dictionary(Of String, String)
296         param.Add("source_screen_name", souceScreenName)
297         param.Add("target_screen_name", targetScreenName)
298
299         Return httpCon.GetContent(GetMethod, _
300                             CreateTwitterUri("/1/friendships/show.json"), _
301                             param, _
302                             content, _
303                             TwitterApiInfo.HttpHeaders, _
304                             AddressOf GetApiCallback)
305     End Function
306
307     Public Function ShowStatuses(ByVal id As Long, ByRef content As String) As HttpStatusCode
308         Return httpCon.GetContent(GetMethod, _
309                             CreateTwitterUri("/1/statuses/show/" + id.ToString() + ".json"), _
310                             Nothing, _
311                             content, _
312                             TwitterApiInfo.HttpHeaders, _
313                             AddressOf GetApiCallback)
314     End Function
315
316     Public Function CreateFavorites(ByVal id As Long, ByRef content As String) As HttpStatusCode
317         Return httpCon.GetContent(PostMethod, _
318                             CreateTwitterUri("/1/favorites/create/" + id.ToString() + ".json"), _
319                             Nothing, _
320                             content, _
321                             Nothing, _
322                             Nothing)
323     End Function
324
325     Public Function DestroyFavorites(ByVal id As Long, ByRef content As String) As HttpStatusCode
326         Return httpCon.GetContent(PostMethod, _
327                             CreateTwitterUri("/1/favorites/destroy/" + id.ToString() + ".json"), _
328                             Nothing, _
329                             content, _
330                             Nothing, _
331                             Nothing)
332     End Function
333
334     Public Function HomeTimeline(ByVal count As Integer, ByVal max_id As Long, ByVal since_id As Long, ByRef content As String) As HttpStatusCode
335         Dim param As New Dictionary(Of String, String)
336         If count > 0 Then
337             param.Add("count", count.ToString())
338         End If
339         If max_id > 0 Then
340             param.Add("max_id", max_id.ToString())
341         End If
342         If since_id > 0 Then
343             param.Add("since_id", since_id.ToString())
344         End If
345
346         param.Add("include_entities", "true")
347
348         Return httpCon.GetContent(GetMethod, _
349                             CreateTwitterUri("/1/statuses/home_timeline.json"), _
350                             param, _
351                             content, _
352                             TwitterApiInfo.HttpHeaders, _
353                             AddressOf GetApiCallback)
354     End Function
355
356     Public Function UserTimeline(ByVal user_id As Long, ByVal screen_name As String, ByVal count As Integer, ByVal max_id As Long, ByVal since_id As Long, ByRef content As String) As HttpStatusCode
357         Dim param As New Dictionary(Of String, String)
358
359         If (user_id = 0 AndAlso String.IsNullOrEmpty(screen_name)) OrElse
360             (user_id <> 0 AndAlso Not String.IsNullOrEmpty(screen_name)) Then Return HttpStatusCode.BadRequest
361
362         If user_id > 0 Then
363             param.Add("user_id", user_id.ToString())
364         End If
365         If Not String.IsNullOrEmpty(screen_name) Then
366             param.Add("screen_name", screen_name)
367         End If
368         If count > 0 Then
369             param.Add("count", count.ToString())
370         End If
371         If max_id > 0 Then
372             param.Add("max_id", max_id.ToString())
373         End If
374         If since_id > 0 Then
375             param.Add("since_id", since_id.ToString())
376         End If
377
378         param.Add("include_rts", "true")
379         param.Add("include_entities", "true")
380
381         Return httpCon.GetContent(GetMethod, _
382                             CreateTwitterUri("/1/statuses/user_timeline.json"), _
383                             param, _
384                             content, _
385                             TwitterApiInfo.HttpHeaders, _
386                             AddressOf GetApiCallback)
387     End Function
388
389     Public Function PublicTimeline(ByVal count As Integer, ByVal max_id As Long, ByVal since_id As Long, ByRef content As String) As HttpStatusCode
390         Dim param As New Dictionary(Of String, String)
391         If count > 0 Then
392             param.Add("count", count.ToString())
393         End If
394         If max_id > 0 Then
395             param.Add("max_id", max_id.ToString())
396         End If
397         If since_id > 0 Then
398             param.Add("since_id", since_id.ToString())
399         End If
400
401         param.Add("include_entities", "true")
402
403         Return httpCon.GetContent(GetMethod, _
404                             CreateTwitterUri("/1/statuses/public_timeline.json"), _
405                             param, _
406                             content, _
407                             TwitterApiInfo.HttpHeaders, _
408                             AddressOf GetApiCallback)
409     End Function
410
411     Public Function Mentions(ByVal count As Integer, ByVal max_id As Long, ByVal since_id As Long, ByRef content As String) As HttpStatusCode
412         Dim param As New Dictionary(Of String, String)
413         If count > 0 Then
414             param.Add("count", count.ToString())
415         End If
416         If max_id > 0 Then
417             param.Add("max_id", max_id.ToString())
418         End If
419         If since_id > 0 Then
420             param.Add("since_id", since_id.ToString())
421         End If
422
423         param.Add("include_entities", "true")
424
425         Return httpCon.GetContent(GetMethod, _
426                             CreateTwitterUri("/1/statuses/mentions.json"), _
427                             param, _
428                             content, _
429                             TwitterApiInfo.HttpHeaders, _
430                             AddressOf GetApiCallback)
431     End Function
432
433     Public Function DirectMessages(ByVal count As Integer, ByVal max_id As Long, ByVal since_id As Long, ByRef content As String) As HttpStatusCode
434         Dim param As New Dictionary(Of String, String)
435         If count > 0 Then
436             param.Add("count", count.ToString())
437         End If
438         If max_id > 0 Then
439             param.Add("max_id", max_id.ToString())
440         End If
441         If since_id > 0 Then
442             param.Add("since_id", since_id.ToString())
443         End If
444
445         Return httpCon.GetContent(GetMethod, _
446                             CreateTwitterUri("/1/direct_messages.json"), _
447                             Nothing, _
448                             content, _
449                             TwitterApiInfo.HttpHeaders, _
450                             AddressOf GetApiCallback)
451     End Function
452
453     Public Function DirectMessagesSent(ByVal count As Integer, ByVal max_id As Long, ByVal since_id As Long, ByRef content As String) As HttpStatusCode
454         Dim param As New Dictionary(Of String, String)
455         If count > 0 Then
456             param.Add("count", count.ToString())
457         End If
458         If max_id > 0 Then
459             param.Add("max_id", max_id.ToString())
460         End If
461         If since_id > 0 Then
462             param.Add("since_id", since_id.ToString())
463         End If
464
465         Return httpCon.GetContent(GetMethod, _
466                             CreateTwitterUri("/1/direct_messages/sent.json"), _
467                             Nothing, _
468                             content, _
469                             TwitterApiInfo.HttpHeaders, _
470                             AddressOf GetApiCallback)
471     End Function
472
473     Public Function Favorites(ByVal count As Integer, ByRef content As String) As HttpStatusCode
474         Dim param As New Dictionary(Of String, String)
475         If count <> 20 Then param.Add("count", count.ToString())
476
477         Return httpCon.GetContent(GetMethod, _
478                             CreateTwitterUri("/1/favorites.json"), _
479                             param, _
480                             content, _
481                             TwitterApiInfo.HttpHeaders, _
482                             AddressOf GetApiCallback)
483     End Function
484
485     Public Function Search(ByVal words As String, ByVal lang As String, ByVal rpp As Integer, ByVal page As Integer, ByVal sinceId As Long, ByRef content As String) As HttpStatusCode
486         Dim param As New Dictionary(Of String, String)
487         If Not String.IsNullOrEmpty(words) Then param.Add("q", words)
488         If Not String.IsNullOrEmpty(lang) Then param.Add("lang", lang)
489         If rpp > 0 Then param.Add("rpp", rpp.ToString())
490         If page > 0 Then param.Add("page", page.ToString())
491         If sinceId > 0 Then param.Add("since_id", sinceId.ToString)
492
493         If param.Count = 0 Then Return HttpStatusCode.BadRequest
494
495         Return httpConVar.GetContent(GetMethod, _
496                                         CreateTwitterSearchUri("/search.atom"), _
497                                         param, _
498                                         content, _
499                                         Nothing, _
500                                         "Tween")
501     End Function
502
503     Public Function FollowerIds(ByVal cursor As Long, ByRef content As String) As HttpStatusCode
504         Dim param As New Dictionary(Of String, String)
505         param.Add("cursor", cursor.ToString())
506
507         Return httpCon.GetContent(GetMethod, _
508                             CreateTwitterUri("/1/followers/ids.json"), _
509                             param, _
510                             content, _
511                             TwitterApiInfo.HttpHeaders, _
512                             AddressOf GetApiCallback)
513     End Function
514
515     Public Function RateLimitStatus(ByRef content As String) As HttpStatusCode
516         Return httpCon.GetContent(GetMethod, _
517                             CreateTwitterUri("/1/account/rate_limit_status.json"), _
518                             Nothing, _
519                             content, _
520                             Nothing, _
521                             Nothing)
522     End Function
523
524     Public Function GetLists(ByVal user As String, ByVal cursor As Long, ByRef content As String) As HttpStatusCode
525         Dim param As New Dictionary(Of String, String)
526         param.Add("cursor", cursor.ToString)
527         Return httpCon.GetContent(GetMethod, _
528                             CreateTwitterUri("/1/" + user + "/lists.json"), _
529                             param, _
530                             content, _
531                             TwitterApiInfo.HttpHeaders, _
532                             AddressOf GetApiCallback)
533     End Function
534
535     Public Function PostListID(ByVal user As String, ByVal list_id As String, ByVal name As String, ByVal mode As String, ByVal description As String, ByRef content As String) As HttpStatusCode
536         Dim param As New Dictionary(Of String, String)
537         If name IsNot Nothing Then param.Add("name", name)
538         If mode IsNot Nothing Then param.Add("mode", mode)
539         If description IsNot Nothing Then param.Add("description", description)
540
541         Return httpCon.GetContent(PostMethod, _
542                                   CreateTwitterUri("/1/" + user + "/lists/" + list_id + ".json"), _
543                                   param, _
544                                   content, _
545                                   Nothing, _
546                                   Nothing)
547     End Function
548
549     Public Function DeleteListID(ByVal user As String, ByVal list_id As String, ByRef content As String) As HttpStatusCode
550         Dim param As New Dictionary(Of String, String)
551         param.Add("_method", "DELETE")
552
553         Return httpCon.GetContent(PostMethod, _
554                                   CreateTwitterUri("/1/" + user + "/lists/" + list_id + ".json"), _
555                                   param, _
556                                   content, _
557                                   Nothing, _
558                                   Nothing)
559     End Function
560
561     Public Function GetListsSubscriptions(ByVal user As String, ByVal cursor As Long, ByRef content As String) As HttpStatusCode
562         Dim param As New Dictionary(Of String, String)
563         param.Add("cursor", cursor.ToString)
564         Return httpCon.GetContent(GetMethod, _
565                             CreateTwitterUri("/1/" + user + "/lists/subscriptions.json"), _
566                             param, _
567                             content, _
568                             TwitterApiInfo.HttpHeaders, _
569                             AddressOf GetApiCallback)
570     End Function
571
572     Public Function GetListsStatuses(ByVal user As String, ByVal list_id As String, ByVal per_page As Integer, ByVal max_id As Long, ByVal since_id As Long, ByRef content As String) As HttpStatusCode
573         '認証なくても取得できるが、protectedユーザー分が抜ける
574         Dim param As New Dictionary(Of String, String)
575         If per_page > 0 Then
576             param.Add("per_page", per_page.ToString())
577         End If
578         If max_id > 0 Then
579             param.Add("max_id", max_id.ToString())
580         End If
581         If since_id > 0 Then
582             param.Add("since_id", since_id.ToString())
583         End If
584
585         Return httpCon.GetContent(GetMethod, _
586                             CreateTwitterUri("/1/" + user + "/lists/" + list_id + "/statuses.json"), _
587                             param, _
588                             content, _
589                             TwitterApiInfo.HttpHeaders, _
590                             AddressOf GetApiCallback)
591     End Function
592
593     Public Function PostLists(ByVal user As String, ByVal listname As String, ByVal isPrivate As Boolean, ByVal description As String, ByRef content As String) As HttpStatusCode
594         Dim mode As String = "public"
595         If isPrivate Then
596             mode = "private"
597         End If
598
599         Dim param As New Dictionary(Of String, String)
600         param.Add("name", listname)
601         param.Add("mode", mode)
602         If Not String.IsNullOrEmpty(description) Then
603             param.Add("description", description)
604         End If
605         Return httpCon.GetContent(PostMethod, _
606                             CreateTwitterUri("/1/" + user + "/lists.json"), _
607                             param, _
608                             content, _
609                             Nothing,
610                             Nothing)
611     End Function
612
613     Public Function GetListMembers(ByVal user As String, ByVal list_id As String, ByVal cursor As Long, ByRef content As String) As HttpStatusCode
614         Dim param As New Dictionary(Of String, String)
615         param.Add("cursor", cursor.ToString())
616         Return httpCon.GetContent(GetMethod, _
617                             CreateTwitterUri("/1/" + user + "/" + list_id + "/members.json"), _
618                             param, _
619                             content, _
620                             TwitterApiInfo.HttpHeaders, _
621                             AddressOf GetApiCallback)
622     End Function
623
624     Public Function PostListMembers(ByVal user As String, ByVal list_id As String, ByVal id As String, ByRef content As String) As HttpStatusCode
625         Dim param As New Dictionary(Of String, String)
626         param.Add("id", id)
627         Return httpCon.GetContent(PostMethod, _
628                             CreateTwitterUri("/1/" + user + "/" + list_id + "/members.json"), _
629                             param, _
630                             content, _
631                             Nothing, _
632                             Nothing)
633     End Function
634
635     Public Function DeleteListMembers(ByVal user As String, ByVal list_id As String, ByVal id As String, ByRef content As String) As HttpStatusCode
636         Dim param As New Dictionary(Of String, String)
637         param.Add("id", id)
638         param.Add("_method", "DELETE")
639         Return httpCon.GetContent(PostMethod, _
640                             CreateTwitterUri("/1/" + user + "/" + list_id + "/members.json"), _
641                             param, _
642                             content, _
643                             Nothing, _
644                             Nothing)
645     End Function
646
647     Public Function GetListMembersID(ByVal user As String, ByVal list_id As String, ByVal id As String, ByRef content As String) As HttpStatusCode
648         Return httpCon.GetContent(GetMethod, _
649                             CreateTwitterUri("/1/" + user + "/" + list_id + "/members/" + id + ".json"), _
650                             Nothing, _
651                             content, _
652                             TwitterApiInfo.HttpHeaders, _
653                             AddressOf GetApiCallback)
654     End Function
655
656     Public Function Statusid_retweeted_by_ids(ByVal statusid As Long, ByVal count As Integer, ByVal page As Integer, ByRef content As String) As HttpStatusCode
657         Dim param As New Dictionary(Of String, String)
658         If count > 0 Then
659             param.Add("count", count.ToString())
660         End If
661         If page > 0 Then
662             param.Add("page", page.ToString())
663         End If
664
665         Return httpCon.GetContent(GetMethod, _
666                             CreateTwitterUri("/1/statuses/" + statusid.ToString + "/retweeted_by/ids.json"), _
667                             param, _
668                             content, _
669                             TwitterApiInfo.HttpHeaders, _
670                             AddressOf GetApiCallback)
671     End Function
672
673     Public Function UpdateProfile(ByVal name As String, ByVal url As String, ByVal location As String, ByVal description As String, ByRef content As String) As HttpStatusCode
674         Dim param As New Dictionary(Of String, String)
675
676         param.Add("name", name)
677         param.Add("url", url)
678         param.Add("location", location)
679         param.Add("description", description)
680
681         Return httpCon.GetContent(PostMethod, _
682                     CreateTwitterUri("/1/account/update_profile.json"), _
683                     param, _
684                     content, _
685                     Nothing, _
686                     Nothing)
687     End Function
688
689     Public Function UpdateProfileImage(ByVal imageFile As FileInfo, ByRef content As String) As HttpStatusCode
690         Dim binary As New List(Of KeyValuePair(Of String, FileInfo))
691         binary.Add(New KeyValuePair(Of String, FileInfo)("image", imageFile))
692
693         Return httpCon.GetContent(PostMethod, _
694                             CreateTwitterUri("/1/account/update_profile_image.json"), _
695                             Nothing, _
696                             binary, _
697                             content, _
698                             Nothing, _
699                             Nothing)
700     End Function
701
702     Public Function GetRelatedResults(ByVal id As Long, ByRef content As String) As HttpStatusCode
703         '認証なくても取得できるが、protectedユーザー分が抜ける
704
705         Return httpCon.GetContent(GetMethod, _
706                             CreateTwitterUri("/1/related_results/show/" + id.ToString + ".json"), _
707                             Nothing, _
708                             content, _
709                             TwitterApiInfo.HttpHeaders, _
710                             AddressOf GetApiCallback)
711     End Function
712
713
714 #Region "Proxy API"
715     Private Shared _twitterUrl As String = "api.twitter.com"
716     Private Shared _TwitterSearchUrl As String = "search.twitter.com"
717     Private Shared _twitterStreamUrl As String = "userstream.twitter.com"
718
719     Private Function CreateTwitterUri(ByVal path As String) As Uri
720         Return New Uri(String.Format("{0}{1}{2}", _protocol, _twitterUrl, path))
721     End Function
722
723     Private Function CreateTwitterSearchUri(ByVal path As String) As Uri
724         Return New Uri(String.Format("{0}{1}{2}", _protocol, _TwitterSearchUrl, path))
725     End Function
726
727     Private Function CreateTwitterStreamUri(ByVal path As String) As Uri
728         Return New Uri(String.Format("{0}{1}{2}", "https://", _twitterStreamUrl, path))
729     End Function
730
731     Public Shared WriteOnly Property TwitterUrl() As String
732         Set(ByVal value As String)
733             _twitterUrl = value
734             HttpOAuthApiProxy.ProxyHost = value
735         End Set
736     End Property
737
738     Public Shared WriteOnly Property TwitterSearchUrl() As String
739         Set(ByVal value As String)
740             _TwitterSearchUrl = value
741         End Set
742     End Property
743 #End Region
744
745     Private Sub GetApiCallback(ByVal sender As Object, ByRef code As HttpStatusCode, ByRef content As String)
746         If code < HttpStatusCode.InternalServerError Then
747             TwitterApiInfo.ParseHttpHeaders(TwitterApiInfo.HttpHeaders)
748         End If
749     End Sub
750
751     Public Function UserStream(ByRef content As Stream,
752                                ByVal allAtReplies As Boolean,
753                                ByVal trackwords As String,
754                                ByVal userAgent As String) As HttpStatusCode
755         Dim param As New Dictionary(Of String, String)
756
757         If allAtReplies Then
758             param.Add("replies", "all")
759         End If
760
761         If Not String.IsNullOrEmpty(trackwords) Then
762             param.Add("track", trackwords)
763         End If
764
765         Return httpCon.GetContent(GetMethod, _
766                             CreateTwitterStreamUri("/2/user.json"), _
767                             param, _
768                             content,
769                             userAgent)
770     End Function
771
772     Public Sub RequestAbort()
773         httpCon.RequestAbort()
774     End Sub
775
776     Public Function Clone() As Object Implements System.ICloneable.Clone
777         Dim myCopy As New HttpTwitter
778         If Me.connectionType = AuthMethod.Basic Then
779             myCopy.Initialize(Me.AuthenticatedUsername, Me.Password)
780         Else
781             myCopy.Initialize(Me.AccessToken, Me.AccessTokenSecret, Me.AuthenticatedUsername)
782         End If
783         Return myCopy
784     End Function
785 End Class