News  [SoftwareSite

Latest News
Older News
RSS Feed
 
Complete Projects
Useful Classes
Top Downloads
Message Board
AllAPI.net
 
Send Comments
Software License
Mentalis.org Buttons
Donate
 
Forums -> Security Library Forum
 
Securesocket receive  
by Mark Hall [mark_hall at cox dot net]
posted on 2003/11/26

I am using a modified webclient from the samples directory. I can send the request and receive the certificate but I cannot receive any data. In the code

byte[] buffer = new byte[4096];
int ret = s.Receive(buffer);
while(ret != 0)
{
Console.Write(Encoding.ASCII.GetString(buffer, 0, ret));
ret = s.Receive(buffer);
}

ret is always 0 so no data is ever recieved. Any workarounds would be helpful.

by Pieter Philippaerts [Pieter at mentalis dot org]
posted on 2003/11/27

What exactly did you modify in the example?
Also, does the original example work with the server you're connecting to?

by Mark Hall [mark_hall at cox dot net]
posted on 2003/11/28

I modified the get statement to a post. here is the code.

Public Class PostClient

Private _url As Url
Private _postData As Byte()
Private _postLength As Long
Private _secureprot As SecureProtocol


Public Sub New()

End Sub

Public Property url() As url
Get
Return _url
End Get
Set(ByVal Value As url)
_url = Value
End Set
End Property

Public Property postData() As Byte()
Get
Return _postData
End Get
Set(ByVal Value As Byte())
_postData = Value
End Set
End Property

Public Property postLength() As Long
Get
Return _postLength
End Get
Set(ByVal Value As Long)
_postLength = Value
End Set
End Property

Public Property secureprot() As SecureProtocol
Get
Return _secureprot
End Get
Set(ByVal Value As SecureProtocol)
_secureprot = Value
End Set
End Property

Public Function ProcessRequest() As String
Return DownloadFile(Me.url, Me.secureprot)

End Function

Public Function DownloadFile(ByVal iUrl As url, ByVal iSp As SecureProtocol) As String
Dim request As String = GetHttpRequest(iUrl)
Dim s As SecureSocket
Dim sb As New StringBuilder
Try
Dim options As SecurityOptions = New SecurityOptions(iSp)
options.Certificate = Nothing
options.Entity = ConnectionEnd.Client
options.CommonName = iUrl.Host
options.VerificationType = CredentialVerification.Manual
options.Verifier = New CertVerifyEventHandler(AddressOf onVerify)
options.Flags = SecurityFlags.Default
options.AllowedAlgorithms = SslAlgorithms.SECURE_CIPHERS
s = New SecureSocket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp, options)
s.Connect(New IPEndPoint((Dns.Resolve(iUrl.Host).AddressList(0)), iUrl.Port))
Catch ex As Exception
Console.WriteLine("Exception occurred while connecting: " + ex.ToString())
Return Nothing
End Try

Try
Dim reqBytes() As Byte = Encoding.ASCII.GetBytes(request)
Dim sent As Integer = s.Send(reqBytes, 0, reqBytes.Length, SocketFlags.None)
Console.WriteLine(reqBytes.Length)

While sent <> reqBytes.Length
Console.WriteLine("ooooooooooooooooooooooooooo")
sent += s.Send(reqBytes, sent, reqBytes.Length - sent, SocketFlags.None)
End While
Catch ex As Exception
Console.WriteLine("Exception occurred while sending: " + ex.ToString())
Return Nothing
End Try

Try
'Console.WriteLine("ooooooooooooooooooooooooooo")
Dim buffer(4096) As Byte
Dim ret As Integer = s.Receive(buffer)
Console.WriteLine(s.Available)
While ret <> 0
'Console.WriteLine(Encoding.ASCII.GetString(buffer, 0, ret))
sb.Append(Encoding.ASCII.GetString(buffer, 0, ret))
ret = s.Receive(buffer)
End While

Catch ex As Exception
Console.WriteLine("Exception occurred while receiving: " + ex.ToString())
Return Nothing
End Try

Try
s.Shutdown(SocketShutdown.Both)
Catch ex As Exception
Console.WriteLine("Exception occurred while shutting the connection down: " + ex.ToString())
Return Nothing
End Try
s.Close()
If sb.ToString = Nothing Then
Return Nothing
Else
Return sb.ToString
End If
End Function

Public Function GetHttpRequest(ByVal iUrl As url) As String
Dim request As String = "POST " & Mid(iUrl.Path, 1, iUrl.Path.Length) + " HTTP/1.1" & ControlChars.CrLf
request += "Accept: */*" & ControlChars.CrLf '& "Accept-Language: en-us" & ControlChars.CrLf

request += "Host: " & iUrl.Host & ControlChars.CrLf
request += "Authorization: Basic " + Convert.ToBase64String(Encoding.ASCII.GetBytes(iUrl.Username + ":" + iUrl.Password)) & ControlChars.CrLf
request += "Content.Length: " & Me.postLength & ControlChars.CrLf
request += "Content-Type: application/x-www-form-urlencoded" & ControlChars.CrLf & ControlChars.CrLf
request += "NETCONNECT_TRANSACTION=" + HttpUtility.UrlEncode(Me.postData)
Return request & ControlChars.CrLf
End Function

Protected Sub onVerify(ByVal socket As SecureSocket, ByVal remote As Certificate, ByVal chain As CertificateChain, ByVal e As VerifyEventArgs)
Dim cc As CertificateChain = New CertificateChain(remote)
Console.WriteLine(remote.ToString(True))
Console.WriteLine(cc.VerifyChain(socket.CommonName, AuthType.Server).ToString())
End Sub
End Class

by Pieter Philippaerts [Pieter at mentalis dot org]
posted on 2003/11/30

Make sure your POST is correct.
If there's a problem with your POST request [for instance if the server expects more bytes such as a CRLF terminator or the like] and your application isn't sending these final bytes, the HTTP server will -- after it hasn't received anything for a while -- close the connection. When this happens, the SecureSocket.Receive method returns 0.
Try sending the POST request to an HTTP server without using a secure connection. If this doesn't work either, you can be sure that it's a problem with your POST query.

by ales [alesv at fbl dot cz]
posted on 2004/12/31

Is there any solution for this problem?
I have same :-(
Ales

 

Copyright © 2002-2007, The Mentalis.org Team. All rights reserved.
This site is located at http://www.mentalis.org/
Send comments to the webmaster.