Results 1 to 9 of 9

Thread: Problem with Function.

Threaded View

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2006
    Posts
    734

    Unhappy Problem with Function.

    Hi guys,
    I am using .NET compact Framework.

    Here's function:
    VB Code:
    1. Public Function UploadFileBinary(ByVal localFile As String, ByVal uploadUrl As String)
    2.         Try
    3.             ' Define the fact we don't want fully trusted certs
    4.             ServicePointManager.CertificatePolicy = New MyCertificateValidation
    5.             ' We want ssl 3
    6.             'ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3
    7.             ' Create a request
    8.             Dim req As HttpWebRequest = WebRequest.Create(uploadUrl)
    9.  
    10.             ' Create a new CredentialCache object and fill it with the network
    11.             ' credentials required to access the server.
    12.             Dim credentials As NetworkCredential = New NetworkCredential("username", "password")
    13.             Dim credentialCache As System.Net.CredentialCache = New CredentialCache
    14.             credentialCache.Add(New System.Uri(uploadUrl), "Basic", credentials)
    15.  
    16.             req.Method = "PUT"
    17.  
    18.             req.Credentials = credentialCache
    19.             'req.AuthenticationLevel = AuthenticationLevel.None
    20.  
    21.             req.AllowWriteStreamBuffering = True
    22.  
    23.             ' Retrieve request stream
    24.             Dim reqStream As Stream = req.GetRequestStream()
    25.             ' Open the local file
    26.             Dim rdr As FileStream = New FileStream(localFile, FileMode.Open)
    27.  
    28.             ' Allocate byte buffer to hold file contents
    29.             Dim inData() As Byte = New Byte(4096) {}
    30.  
    31.             ' loop through the local file reading each data block
    32.             '  and writing to the request stream buffer
    33.             Dim bytesRead As Integer = rdr.Read(inData, 0, inData.Length)
    34.             While (bytesRead > 0)
    35.                 reqStream.Write(inData, 0, bytesRead)
    36.                 bytesRead = rdr.Read(inData, 0, inData.Length)
    37.             End While
    38.  
    39.             rdr.Close()
    40.             reqStream.Close()
    41.  
    42.             'req.AuthenticationLevel = AuthenticationLevel.None
    43.  
    44.             req.GetResponse()
    45.             Return True
    46.         Catch webEx As WebException
    47.             If (webEx.Status = WebExceptionStatus.ProtocolError) Then
    48.                 Debug.WriteLine(TAB & "Web Exception caught in Upload.UploadFileBinary() - Authorisation Failed" & NL & TAB & webEx.Message.ToString)
    49.             Else
    50.                 Debug.WriteLine(TAB & "Web Exception caught in Upload.UploadFileBinary()." & NL & TAB & webEx.Message.ToString)
    51.             End If
    52.             Return False
    53.         Catch ex As Exception
    54.             Debug.WriteLine(TAB & "Exception caught in Upload.UploadFileBinary()." & NL & TAB & ex.Message.ToString)
    55.             Return False
    56.         End Try
    57.     End Function

    The problem i am getting is when i try to upload some files (they are XML files) i can upload 2 but then when it comes to the third it fails to upload it and my whole system stops responding (I'm guessing this may be because it is waiting for the .getresponse() to time-out (but i'm not 100% sure)). I have very little knowledge in this area and have been given this code by a colleague, so i was wondering whether someone can see where my problem is occuring and how i may be able to fix it because as i said, i have very little knowledge in this area and can't seem to find the problem myself.

    Thanks in advance for any help


    Edit: If it helps anyone...here's the exception that gets caught:
    Web Exception caught in Upload.UploadFileBinary().
    An error message cannot be displayed because an optional resource assembly containing it cannot be found
    Last edited by Kimmy4; Nov 20th, 2006 at 10:19 AM.
    If your problem has been solved then please mark the thread [RESOLVED].
    If i have helped then please Rate my post

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width