views:

59

answers:

4

I'm trying to download an InfoPath template that's hosted on SharePoint. If I hit the url in internet explorer it asks me where to save it and I get the correct file on my disk. If I try to do this programmatically with WebClient or HttpWebRequest then I get HTML back instead.

How can I make my request so that the web server returns the actual xsn file and doesn't try to render it in html. If internet explorer can do this then it's logical to think that I can too.

I've tried setting the Accept property of the request to application/x-microsoft-InfoPathFormTemplate but that hasn't helped. It was a shot in the dark.

A: 

Have you tried spoofing Internet Explorer's User-Agent?

jleedev
A: 

There is a HTTP response header that makes a HTTP user agent download a file instead of trying to display it:

Content-Disposition: attachment; filename=paper.doc

I understand that you may not have access to the server, but this is one straight-forward way to do this if you can access the server scripts.

See the HTTP/1.1 specification and/or say, Google, for more details on the header.

amn
A: 

This is vb.net, but you should get the point. I've done this with an .aspx page that you pass the filename into, then return the content type of the file and add a header to make it an attachment, which prompts the browser to treat it as such.

Response.AddHeader("Content-Disposition", "attachment;filename=filename.xsn")
Response.ContentType = "application/x-microsoft-InfoPathFormTemplate"
Response.WriteFile(FilePath)  ''//Where FilePath is... the path to your file ;)
Response.Flush()
Response.End()
Shawn Steward
The difference is, I am the browser in this case. I'm not the server so I don't control the response. All I can do is change my request to try and make the server return the file itself. Except I don't know how to do this.
Dan Revell
You can try to use a proxy server that you do control, which WILL add the mentioned header to a response, prior to returning it to your agent.
amn
@amn -- but if the original server has already altered the file before it reaches your proxy, the game is already lost.
James Curran
James, am I missing something here? A HTTP server that matches a URL to a file does not modify anything, it just sends the file data as the body of the response, along with some headers. A proxy is well, a proxy - when your agent requests the URL through the proxy, the proxy will make request to the origin server, get the response, add the "Content-Disposition" header and return the modified response to the agent. Am i missing something?
amn
@amn You are missing the fact that the web server does do something to the file. You can't always assume a uri is a physical file on a web server. In my case the difference is that manually the physical file (well a file in the sharepoint database) gets returned and programmatically infopath form services renders the form into html and returns that. I'm trying to discover what the difference is in the two requests that makes the web server do these different things.
Dan Revell
Hmm, I see. Well then I will join the others in taking a chance on replicating IE request behavior. Obviously, it all has to do with that. IE sends simple plaintext as the request, so there is no magic involved - just send the same request (use Fiddler to spoof it). Sorry, I could not help you otherwise.
amn
+1  A: 

I'd suggest using Fiddler or WireShark, to see exactly how IE is sending the request, then duplicating that.

James Curran