Results 1 to 9 of 9

Thread: Problem with Function.

  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

  2. #2

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2006
    Posts
    734

    Re: Problem with Function.

    I added debug messages after each line in the code and it stops it never displays the debug line after the line of code
    VB Code:
    1. req.GetResponse()
    but only on the third file. It displays the message for the first two. I tried running the code with only 2 files to upload and they upload perfectly so i tried it with 4 files and it fails on the third again!
    I'm doing a couple of more tests so i can let you know of any other results i find, but if anyone thinks they know what my problem may be then i'd be really grateful of any input.
    Thanks guys
    If your problem has been solved then please mark the thread [RESOLVED].
    If i have helped then please Rate my post

  3. #3
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: Problem with Function.

    Try wrapping it around an If block and check to see if there is a response first....
    VB Code:
    1. If req.HaveResponse() Then
    2.                 req.GetResponse()
    3.             End If

  4. #4

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2006
    Posts
    734

    Re: Problem with Function.

    Could you explain to me why i would need to do this as it may help me understand the process of uploading using http even more. I'll try it out and let you know what i get.
    Thanks for your help.
    If your problem has been solved then please mark the thread [RESOLVED].
    If i have helped then please Rate my post

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2006
    Posts
    734

    Re: Problem with Function.

    Stanav,
    I tried what you suggested and it didn't upload anything at all that time!
    If your problem has been solved then please mark the thread [RESOLVED].
    If i have helped then please Rate my post

  6. #6

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2006
    Posts
    734

    Re: Problem with Function.

    *bump*
    If your problem has been solved then please mark the thread [RESOLVED].
    If i have helped then please Rate my post

  7. #7

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2006
    Posts
    734

    Re: Problem with Function.

    I have added a few more message boxes to try and find out whats happening and when i add a message after the line
    VB Code:
    1. Dim bytesRead As Integer = rdr.Read(inData, 0, inData.Length)
    to display what bytesRead is initialised to it returns the size of the xml file it is trying to upload and then i added a debug message inside the while loop and only one message displays and this shows bytesRead = 0.
    Don't know whether this will help at all but i really need this problem sorted asap so i thought i's supply all the information i know!
    It turns out the web request isn't even hitting the server!
    Please can anyone help?
    If your problem has been solved then please mark the thread [RESOLVED].
    If i have helped then please Rate my post

  8. #8

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2006
    Posts
    734

    Re: Problem with Function.

    *bump*
    If your problem has been solved then please mark the thread [RESOLVED].
    If i have helped then please Rate my post

  9. #9

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2006
    Posts
    734

    Re: Problem with Function.

    I am trying to work around my problem by only uploading a certain amount at a time, but if someone could possibly tell me why it always fails on the third upload i would really appreciate it, because i can't think of any reason why it would be failing when it has uploaded 2 files successfully directly before trying the third one???

    Can anyone please help
    Thanks
    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