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
| Imports System.ComponentModel
Imports System.Net
Imports System.IO
Public Class Core
Implements INotifyPropertyChanged
Public Sub New(ByVal AccountName As String, ByVal Password As String)
If String.IsNullOrEmpty(AccountName) Then Throw New ArgumentNullException("AccountName")
If String.IsNullOrEmpty(Password) Then Throw New ArgumentNullException("Password")
strAccountName = AccountName
strPassword = Password
End Sub
#Region "Propriétés"
Private strAccountName As String
Public ReadOnly Property AccountName() As String
Get
Return strAccountName
End Get
End Property
Private strPassword As String
Public ReadOnly Property Password() As String
Get
Return strPassword
End Get
End Property
Private ReadOnly Property FreeUrl As String
Get
Return String.Format("http://subscribe.free.fr/login/login.pl?login={0}&pass={1}", Me.AccountName, Me.Password)
End Get
End Property
#End Region
Public Function SendFax(ByVal Document As FreeFax.Document) As FreeFax.OperationResult
Dim id = String.Empty
Dim idt = String.Empty
Dim request As HttpWebRequest = WebRequest.Create(FreeUrl)
request.Method = "GET"
Dim response As HttpWebResponse = request.GetResponse()
If response.StatusCode <> HttpStatusCode.OK Then Return FreeFax.OperationResult.ConnectionFailure
Dim source As New IO.StreamReader(response.GetResponseStream)
Dim sourceText As String = source.ReadToEnd
response.Close()
If GetFaxURL(sourceText, id, idt) = False Then Return FreeFax.OperationResult.ConnectionFailure
Dim DestUrl = "https://adsl.free.fr/admin/tel/fax/tel_ulfax.pl"
Dim collection As New Specialized.NameValueCollection
collection.Add("id", id)
collection.Add("idt", idt)
collection.Add("destinataire", Document.ToNumber)
collection.Add("email_ack", "1")
Dim FilePath = "D:\Bureau\test.pdf"
Dim Faxresponse = UploadFilesToRemoteUrl(DestUrl, New String() {FilePath}, collection)
Return FreeFax.OperationResult.Succeed
End Function
Private Function GetFaxURL(ByVal Html As String, ByRef id As String, ByRef idt As String) As Boolean
Try
Dim PatternString = "https://assistance.free.fr/compte/index.php?id="
Dim FirstIndex = Html.IndexOf(PatternString) + PatternString.Length
Dim SecondIndex = Html.IndexOf("&", FirstIndex)
Dim ThirdIndex = Html.IndexOf(Chr(34), SecondIndex)
id = Html.Substring(FirstIndex, SecondIndex - FirstIndex)
idt = Html.Substring(SecondIndex + 5, ThirdIndex - SecondIndex - 5)
Catch ex As Exception
Return False
End Try
Return True
End Function
Private Function UploadFilesToRemoteUrl(ByVal url As String, ByVal files As String(), ByVal nvc As Specialized.NameValueCollection) As String
Dim length As Long = 0
Dim boundary = "-----------------------------" & DateTime.Now.Ticks.ToString("x")
Dim httpWebRequest2 As HttpWebRequest = WebRequest.Create(url)
httpWebRequest2.ContentType = "multipart/form-data; boundary=" & boundary
httpWebRequest2.Method = "POST"
httpWebRequest2.KeepAlive = True
Dim memStream As New System.IO.MemoryStream
Dim boundarybytes As Byte() = System.Text.Encoding.ASCII.GetBytes(Environment.NewLine & "--" & boundary & Environment.NewLine & Environment.NewLine)
Dim formdataTemplate = Environment.NewLine & "--" & boundary & Environment.NewLine & "Content-Disposition: form-data; name=""{0}""" & Environment.NewLine & Environment.NewLine & "{1}"
For Each key As String In nvc.Keys
Dim formItem As String = String.Format(formdataTemplate, key, nvc.Item(key))
Dim formItemBytes As Byte() = System.Text.Encoding.UTF8.GetBytes(formItem)
memStream.Write(formItemBytes, 0, formItemBytes.Length)
Next
memStream.Write(boundarybytes, 0, boundarybytes.Length)
Dim headerTemplate = "Content-Disposition: form-data; name=""{0}""; filename=""{1}""" & Environment.NewLine & "Content-Type: application/pdf" & Environment.NewLine & Environment.NewLine
For i = 0 To files.Length - 1
Dim header = String.Format(headerTemplate, "document", files(i).Substring(files(i).LastIndexOf("\") + 1))
Dim headerBytes As Byte() = System.Text.Encoding.UTF8.GetBytes(header)
memStream.Write(headerBytes, 0, headerBytes.Length)
Dim fileStream As IO.FileStream = New IO.FileStream(files(i), IO.FileMode.Open, IO.FileAccess.Read)
Dim buffer As Byte() = New Byte(1024) {}
Dim bytesRead As Integer = fileStream.Read(buffer, 0, buffer.Length)
Do While bytesRead > 0
memStream.Write(buffer, 0, bytesRead)
bytesRead = fileStream.Read(buffer, 0, buffer.Length)
Loop
memStream.Write(boundarybytes, 0, boundarybytes.Length)
fileStream.Close()
Next
Dim requestStream = httpWebRequest2.GetRequestStream
memStream.Position = 0
Dim tempBuffer As Byte() = New Byte(memStream.Length - 1) {}
memStream.Read(tempBuffer, 0, tempBuffer.Length)
'Debug
Dim content = memStream.ReadAll
memStream.Close()
requestStream.Write(tempBuffer, 0, tempBuffer.Length)
requestStream.Close()
Dim webResponse As WebResponse = httpWebRequest2.GetResponse
Dim stream2 As IO.Stream = webResponse.GetResponseStream
Dim reader2 As New IO.StreamReader(stream2)
Dim result = reader2.ReadToEnd
webResponse.Close()
httpWebRequest2 = Nothing
webResponse = Nothing
Return result
End Function
Public Event PropertyChanged(ByVal sender As Object, ByVal e As System.ComponentModel.PropertyChangedEventArgs) Implements System.ComponentModel.INotifyPropertyChanged.PropertyChanged
End Class |
Partager