Programmatically uploading a file via WebDAV using HttpWebRequest/FileWebRequest

S

Shannon Hardin

I'm trying to build a routine that will take a specified file from the
user's local hard drive, and copy it to a remote server via WebDAV. I've
tried using both HttpWebRequest and FileWebRequest, as outlined in some
samples I found on MSDN and elsewhere, but so far have had no luck
whatsoever. The copy process doesn't fail, but neither does the targeted
file actually appear on the destination server. I have verified that I can
copy files manually to the target WebDAV folder, via a mapped network
resource on Windows XP. Word XP successfully loads and saves files to the
target folder as well.

Here's the code I'm using:

Dim MyResponse As HttpWebResponse
Dim MyCredentialCache = New System.Net.CredentialCache
Dim UserName As String = "MyUserName"
Dim Password As String = "MyPassword"
Dim Domain As String = "MyDomain"
Dim SourceURI As String = "http://localhost/text.xml"
Dim MyRequest As HttpWebRequest = CType(WebRequest.Create(SourceURI),
HttpWebRequest)

Try

MyCredentialCache.Add(New System.Uri(SourceURI), "NTLM", New
System.Net.NetworkCredential(UserName, Password, Domain))

' Add the network credentials to the request.
MyRequest.Credentials = MyCredentialCache
MyRequest.Headers.Add("Destination",
"http://DestinationServer/SubFolder/text.xml")
MyRequest.Headers.Add("Overwrite", "F")
MyRequest.Method = "COPY"
MyResponse = CType(MyRequest.GetResponse(), HttpWebResponse)
MyResponse.Close()

Catch ex As Exception
MsgBox(ex.Message)
End Try

The status code contained in the response object will say "Created" every
time, but no actual file appears. I have also used a slightly modified
version of this code that uses FileWebRequest instead of HttpWebRequest,
with similar results. Essentially, in that case, the response object
contains a stream that appears to have all the data from the source file in
it, but the file itself never appears in the target folder. Any insight
would be greatly appreciated!

Thanks,

Shannon
 
M

Markus Hahn

I'm not sure, bu I think you must also stream the data up to the server. But that's just a guess.
If you're not sure what's happening behind the doors then I'd try a packet sniffer and monitor the actual request going out - there
you should see if your file data actually makes it over the wire. Try www.ethereal.com for a nice freeware packet capture.

-markus
 
S

Shannon Hardin

Markus,

I found the solution, and you are correct. It is the responsibility of the
client to also stream the data to the server. Unfortunately, Microsoft's
own article and sample code fail to mention this small fact, for some
reason.

I actually found an even simpler way to do these types of copy. ADO with
the Internet Publishing provider can handle it, and that's the method I
went with.

Thanks,

Shannon

Markus Hahn said:
I'm not sure, bu I think you must also stream the data up to the server. But that's just a guess.
If you're not sure what's happening behind the doors then I'd try a packet
sniffer and monitor the actual request going out - there
you should see if your file data actually makes it over the wire. Try
www.ethereal.com for a nice freeware packet capture.
 
S

santoshpotdar

Hello Shannon,

I have been trying to do the same thing and I am getting
the same error.
Can u help me with eth esolution u have found.

Thanks in advance.

Regards,
Santosh



Shannon said:
*I'm trying to build a routine that will take a specified file fro
the
user's local hard drive, and copy it to a remote server via WebDAV.
I've
tried using both HttpWebRequest and FileWebRequest, as outlined i
some
samples I found on MSDN and elsewhere, but so far have had no luck
whatsoever. The copy process doesn't fail, but neither does th
targeted
file actually appear on the destination server. I have verified tha
I can
copy files manually to the target WebDAV folder, via a mappe
network
resource on Windows XP. Word XP successfully loads and saves file
to the
target folder as well.

Here's the code I'm using:

Dim MyResponse As HttpWebResponse
Dim MyCredentialCache = New System.Net.CredentialCache
Dim UserName As String = "MyUserName"
Dim Password As String = "MyPassword"
Dim Domain As String = "MyDomain"
Dim SourceURI As String = "http://localhost/text.xml"
Dim MyRequest As HttpWebRequest
CType(WebRequest.Create(SourceURI),
HttpWebRequest)

Try

MyCredentialCache.Add(New System.Uri(SourceURI), "NTLM", New
System.Net.NetworkCredential(UserName, Password, Domain))

' Add the network credentials to the request.
MyRequest.Credentials = MyCredentialCache
MyRequest.Headers.Add("Destination",
"http://DestinationServer/SubFolder/text.xml")
MyRequest.Headers.Add("Overwrite", "F")
MyRequest.Method = "COPY"
MyResponse = CType(MyRequest.GetResponse(), HttpWebResponse)
MyResponse.Close()

Catch ex As Exception
MsgBox(ex.Message)
End Try

The status code contained in the response object will say "Created
every
time, but no actual file appears. I have also used a slightl
modified
version of this code that uses FileWebRequest instead o
HttpWebRequest,
with similar results. Essentially, in that case, the respons
object
contains a stream that appears to have all the data from the sourc
file in
it, but the file itself never appears in the target folder. An
insight
would be greatly appreciated!

Thanks,

Shannon


-
santoshpotda
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top