Connecting Tech Pros Worldwide Forums | Help | Site Map

Programmatically uploading a file via WebDAV using HttpWebRequest/FileWebRequest

Shannon Hardin
Guest
 
Posts: n/a
#1: Nov 20 '05
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



Markus Hahn
Guest
 
Posts: n/a
#2: Nov 20 '05

re: Programmatically uploading a file via WebDAV using HttpWebRequest/FileWebRequest


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


"Shannon Hardin" <shardin@alginc.com> wrote in message news:unSMWKo5DHA.2008@TK2MSFTNGP10.phx.gbl...[color=blue]
> 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
>
>[/color]


Shannon Hardin
Guest
 
Posts: n/a
#3: Nov 20 '05

re: Programmatically uploading a file via WebDAV using HttpWebRequest/FileWebRequest


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" <markus_hahn@gmx.net> wrote in message
news:3RpSb.2662$uM2.651@newsread1.news.pas.earthli nk.net...[color=blue]
> I'm not sure, bu I think you must also stream the data up to the server.[/color]
But that's just a guess.[color=blue]
> If you're not sure what's happening behind the doors then I'd try a packet[/color]
sniffer and monitor the actual request going out - there[color=blue]
> you should see if your file data actually makes it over the wire. Try[/color]
www.ethereal.com for a nice freeware packet capture.[color=blue]
>
> -markus
>
>
> "Shannon Hardin" <shardin@alginc.com> wrote in message[/color]
news:unSMWKo5DHA.2008@TK2MSFTNGP10.phx.gbl...[color=blue][color=green]
> > 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.[/color][/color]
I've[color=blue][color=green]
> > 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[/color][/color]
targeted[color=blue][color=green]
> > file actually appear on the destination server. I have verified that I[/color][/color]
can[color=blue][color=green]
> > copy files manually to the target WebDAV folder, via a mapped network
> > resource on Windows XP. Word XP successfully loads and saves files to[/color][/color]
the[color=blue][color=green]
> > 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 =[/color][/color]
CType(WebRequest.Create(SourceURI),[color=blue][color=green]
> > 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"[/color][/color]
every[color=blue][color=green]
> > 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[/color][/color]
in[color=blue][color=green]
> > it, but the file itself never appears in the target folder. Any insight
> > would be greatly appreciated!
> >
> > Thanks,
> >
> > Shannon
> >
> >[/color]
>
>[/color]


santoshpotdar
Guest
 
Posts: n/a
#4: Nov 21 '05

re: Programmatically uploading a file via WebDAV using HttpWebRequest/FileWebRequest



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 Hardin wrote:[color=blue]
> *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[/color]


-
santoshpotda
-----------------------------------------------------------------------
Posted via http://www.mcse.m
-----------------------------------------------------------------------
View this thread: http://www.mcse.ms/message342150.htm

Terry Olsen
Guest
 
Posts: n/a
#5: Nov 21 '05

re: Programmatically uploading a file via WebDAV using HttpWebRequest/FileWebRequest


Here is the method I used and it works well.

http://support.microsoft.com/default...b;en-us;323245


"santoshpotdar" <santoshpotdar.1tneu5@mail.mcse.ms> wrote in message
news:santoshpotdar.1tneu5@mail.mcse.ms...[color=blue]
>
> 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 Hardin wrote:[color=green]
>> *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 *[/color]
>
>
>
> --
> santoshpotdar
> ------------------------------------------------------------------------
> Posted via http://www.mcse.ms
> ------------------------------------------------------------------------
> View this thread: http://www.mcse.ms/message342150.html
>[/color]


Closed Thread