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>
9 ' This file is part of Tween.
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)
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
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.
29 Imports System.Diagnostics
35 '''使用前にユーザー/パスワードを設定する。認証確認を伴う場合はAuthenticateを、認証不要な場合はInitializeを呼ぶこと。
37 Public Class HttpConnectionBasic
38 Inherits HttpConnection
39 Implements IHttpConnection
44 Private _userName As String = ""
48 Private _password As String = ""
50 '''Authorizationヘッダに設定するエンコード済み文字列
52 Private credential As String = ""
56 '''認証完了時の応答からuserIdentKey情報に基づいて取得するユーザー情報
58 Private streamReq As HttpWebRequest = Nothing
61 '''BASIC認証で指定のURLとHTTP通信を行い、結果を返す
63 '''<param name="method">HTTP通信メソッド(GET/HEAD/POST/PUT/DELETE)</param>
64 '''<param name="requestUri">通信先URI</param>
65 '''<param name="param">GET時のクエリ、またはPOST時のエンティティボディ</param>
66 '''<param name="content">[OUT]HTTP応答のボディデータ</param>
67 '''<param name="headerInfo">[IN/OUT]HTTP応答のヘッダ情報。必要なヘッダ名を事前に設定しておくこと</param>
68 '''<param name="callback">処理終了直前に呼ばれるコールバック関数のデリゲート 不要な場合はNothingを渡すこと</param>
69 '''<returns>HTTP応答のステータスコード</returns>
70 Public Function GetContent(ByVal method As String, _
71 ByVal requestUri As Uri, _
72 ByVal param As Dictionary(Of String, String), _
73 ByRef content As String, _
74 ByVal headerInfo As Dictionary(Of String, String), _
75 ByVal callback As IHttpConnection.CallbackDelegate) As HttpStatusCode Implements IHttpConnection.GetContent
78 If String.IsNullOrEmpty(Me.credential) Then Return HttpStatusCode.Unauthorized
80 Dim webReq As HttpWebRequest = CreateRequest(method, _
87 Dim code As HttpStatusCode
88 If content Is Nothing Then
89 code = GetResponse(webReq, headerInfo, False)
91 code = GetResponse(webReq, content, headerInfo, False)
93 If callback IsNot Nothing Then
94 Dim frame As New StackFrame(1)
95 callback(frame.GetMethod.Name, code, content)
100 Public Function GetContent(ByVal method As String, _
101 ByVal requestUri As Uri, _
102 ByVal param As Dictionary(Of String, String), _
103 ByVal binary As List(Of KeyValuePair(Of String, FileInfo)), _
104 ByRef content As String, _
105 ByVal headerInfo As Dictionary(Of String, String), _
106 ByVal callback As IHttpConnection.CallbackDelegate) As HttpStatusCode Implements IHttpConnection.GetContent
109 If String.IsNullOrEmpty(Me.credential) Then Return HttpStatusCode.Unauthorized
111 Dim webReq As HttpWebRequest = CreateRequest(method, _
117 AppendApiInfo(webReq)
119 Dim code As HttpStatusCode
120 If content Is Nothing Then
121 code = GetResponse(webReq, headerInfo, False)
123 code = GetResponse(webReq, content, headerInfo, False)
125 If callback IsNot Nothing Then
126 Dim frame As New StackFrame(1)
127 callback(frame.GetMethod.Name, code, content)
133 '''OAuth認証で指定のURLとHTTP通信を行い、ストリームを返す
135 '''<param name="method">HTTP通信メソッド(GET/HEAD/POST/PUT/DELETE)</param>
136 '''<param name="requestUri">通信先URI</param>
137 '''<param name="param">GET時のクエリ、またはPOST時のエンティティボディ</param>
138 '''<param name="content">[OUT]HTTP応答のボディストリーム</param>
139 '''<param name="headerInfo">[IN/OUT]HTTP応答のヘッダ情報。必要なヘッダ名を事前に設定しておくこと</param>
140 '''<returns>HTTP応答のステータスコード</returns>
141 Public Function GetContent(ByVal method As String, _
142 ByVal requestUri As Uri, _
143 ByVal param As Dictionary(Of String, String), _
144 ByRef content As Stream,
145 ByVal userAgent As String) As HttpStatusCode Implements IHttpConnection.GetContent
147 If String.IsNullOrEmpty(Me.credential) Then Return HttpStatusCode.Unauthorized
149 streamReq = CreateRequest(method, requestUri, param, False)
150 'User-Agent指定がある場合は付加
151 If Not String.IsNullOrEmpty(userAgent) Then streamReq.UserAgent = userAgent
154 AppendApiInfo(streamReq)
157 Dim webRes As HttpWebResponse = CType(streamReq.GetResponse(), HttpWebResponse)
158 content = webRes.GetResponseStream()
159 Return webRes.StatusCode
160 Catch ex As WebException
161 If ex.Status = WebExceptionStatus.ProtocolError Then
162 Dim res As HttpWebResponse = DirectCast(ex.Response, HttpWebResponse)
163 Return res.StatusCode
170 Public Sub RequestAbort() Implements IHttpConnection.RequestAbort
172 If streamReq IsNot Nothing Then
175 Catch ex As Exception
180 '''BASIC認証とREST APIで必要なヘッダを付加
182 '''<param name="webRequest">付加対象となるHTTPリクエストオブジェクト</param>
183 Private Sub AppendApiInfo(ByVal webRequest As HttpWebRequest)
184 webRequest.ContentType = "application/x-www-form-urlencoded"
185 webRequest.Accept = "text/html, */*"
186 webRequest.Headers.Add(HttpRequestHeader.Authorization, credential)
190 '''BASIC認証で使用するユーザー名とパスワードを設定。
192 '''<param name="userName">認証で使用するユーザー名</param>
193 '''<param name="password">認証で使用するパスワード</param>
194 Public Sub Initialize(ByVal userName As String, ByVal password As String)
195 Me._userName = userName
196 Me._password = password
197 Me.credential = "Basic " + Convert.ToBase64String(Encoding.ASCII.GetBytes(userName + ":" + password))
203 Public ReadOnly Property AuthUsername() As String Implements IHttpConnection.AuthUsername
212 Public ReadOnly Property Password() As String
219 '''BASIC認証で使用するユーザー名とパスワードを設定。指定のURLにGETリクエストを投げて、OK応答なら認証OKとみなして認証情報を保存する
221 '''<param name="url">認証先のURL</param>
222 '''<param name="userName">認証で使用するユーザー名</param>
223 '''<param name="password">認証で使用するパスワード</param>
224 Public Function Authenticate(ByVal url As Uri, ByVal username As String, ByVal password As String, ByRef content As String) As HttpStatusCode Implements IHttpConnection.Authenticate
226 Dim orgCre As String = Me.credential
227 Me.credential = "Basic " + Convert.ToBase64String(Encoding.ASCII.GetBytes(username + ":" + password))
229 Dim httpCode As HttpStatusCode = Me.GetContent("GET", url, Nothing, Nothing, Nothing, Nothing)
230 If httpCode = HttpStatusCode.OK Then
231 Me._userName = username
232 Me._password = password
234 Me.credential = orgCre
237 Catch ex As Exception
238 Me.credential = orgCre