|
-
Nov 20th, 2006, 09:57 AM
#1
Thread Starter
Fanatic Member
Problem with Function.
Hi guys,
I am using .NET compact Framework.
Here's function:
VB Code:
Public Function UploadFileBinary(ByVal localFile As String, ByVal uploadUrl As String)
Try
' Define the fact we don't want fully trusted certs
ServicePointManager.CertificatePolicy = New MyCertificateValidation
' We want ssl 3
'ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3
' Create a request
Dim req As HttpWebRequest = WebRequest.Create(uploadUrl)
' Create a new CredentialCache object and fill it with the network
' credentials required to access the server.
Dim credentials As NetworkCredential = New NetworkCredential("username", "password")
Dim credentialCache As System.Net.CredentialCache = New CredentialCache
credentialCache.Add(New System.Uri(uploadUrl), "Basic", credentials)
req.Method = "PUT"
req.Credentials = credentialCache
'req.AuthenticationLevel = AuthenticationLevel.None
req.AllowWriteStreamBuffering = True
' Retrieve request stream
Dim reqStream As Stream = req.GetRequestStream()
' Open the local file
Dim rdr As FileStream = New FileStream(localFile, FileMode.Open)
' Allocate byte buffer to hold file contents
Dim inData() As Byte = New Byte(4096) {}
' loop through the local file reading each data block
' and writing to the request stream buffer
Dim bytesRead As Integer = rdr.Read(inData, 0, inData.Length)
While (bytesRead > 0)
reqStream.Write(inData, 0, bytesRead)
bytesRead = rdr.Read(inData, 0, inData.Length)
End While
rdr.Close()
reqStream.Close()
'req.AuthenticationLevel = AuthenticationLevel.None
req.GetResponse()
Return True
Catch webEx As WebException
If (webEx.Status = WebExceptionStatus.ProtocolError) Then
Debug.WriteLine(TAB & "Web Exception caught in Upload.UploadFileBinary() - Authorisation Failed" & NL & TAB & webEx.Message.ToString)
Else
Debug.WriteLine(TAB & "Web Exception caught in Upload.UploadFileBinary()." & NL & TAB & webEx.Message.ToString)
End If
Return False
Catch ex As Exception
Debug.WriteLine(TAB & "Exception caught in Upload.UploadFileBinary()." & NL & TAB & ex.Message.ToString)
Return False
End Try
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 
-
Nov 20th, 2006, 12:12 PM
#2
Thread Starter
Fanatic Member
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
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 
-
Nov 20th, 2006, 12:30 PM
#3
Re: Problem with Function.
Try wrapping it around an If block and check to see if there is a response first....
VB Code:
If req.HaveResponse() Then
req.GetResponse()
End If
-
Nov 20th, 2006, 12:34 PM
#4
Thread Starter
Fanatic Member
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 
-
Nov 20th, 2006, 12:40 PM
#5
Thread Starter
Fanatic Member
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 
-
Nov 21st, 2006, 04:26 AM
#6
Thread Starter
Fanatic Member
Re: Problem with Function.
If your problem has been solved then please mark the thread [RESOLVED].
If i have helped then please Rate my post 
-
Nov 21st, 2006, 06:42 AM
#7
Thread Starter
Fanatic Member
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:
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 
-
Nov 21st, 2006, 11:38 AM
#8
Thread Starter
Fanatic Member
Re: Problem with Function.
If your problem has been solved then please mark the thread [RESOLVED].
If i have helped then please Rate my post 
-
Nov 27th, 2006, 07:10 AM
#9
Thread Starter
Fanatic Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|