HttpWebRequest/Response with e-mail attachments

  • Thread starter Thread starter tomer
  • Start date Start date
T

tomer

Clear DayHello,

I have implemented a download manger in .NET that overrides Internet
Explorer's bult-in download manager.

I using HttpWebRequest/Response to download the files.
All is working well except when I try to download an attachment file from a
web-based mailbox.

I've tried using credentials and saving the cookies, nothing helps.

Please try and assist me in this matter.
Tomer.
 
tomer said:
Clear DayHello,

I have implemented a download manger in .NET that overrides Internet
Explorer's bult-in download manager.

I using HttpWebRequest/Response to download the files.
All is working well except when I try to download an attachment file
from a web-based mailbox.

I've tried using credentials and saving the cookies, nothing helps.

Well, post some code and describe the problem in more detail...

Cheers,
 
Well,

The URL to resolve is something like this:
http://some.domain.com/?w=/@attach&fid=-1000&id=4555055&aid=1&n=&save=1&filename=Brusselse.wmv

HttpWebRequest request;

request = (HttpWebRequest)HttpWebRequest.Create(url);

request.AllowAutoRedirect=false;


request.Method = "GET";

CredentialCache wrCache = new CredentialCache();

wrCache.Add(new Uri(url),"Basic",new NetworkCredential(username,password));

request.Credentials = wrCache;

HttpWebResponse response = (HttpWebResponse)request.GetResponse();

string newurl = GetRedirectURL(response); // it goes trough one
redirection, this function gets the new URL



Than I again do "request/response" with new url, and end up downloading HTML
page source code of some unknown page in the domain. (Maybe the logon page).
 
tomer said:
Well,

The URL to resolve is something like this:
http://some.domain.com/?w=/@attach&fid=-1000&id=4555055&aid=1&n=&save=1&filename=Brusselse.wmv

HttpWebRequest request;

request = (HttpWebRequest)HttpWebRequest.Create(url);

request.AllowAutoRedirect=false;

Out of curiosity: Why do need to implement your own redirection handling?

request.Method = "GET";

CredentialCache wrCache = new CredentialCache();

wrCache.Add(new Uri(url),"Basic",new
NetworkCredential(username,password));
request.Credentials = wrCache;

HttpWebResponse response = (HttpWebResponse)request.GetResponse();

string newurl = GetRedirectURL(response); // it goes trough one
redirection, this function gets the new URL

But what exactly happens at this point? Are you sure you're not losing your
credentials or a session cookie?

Cheers,
 
Hi Joerg,

I'm applying credentials and all setting again on the second request..
As for the Redirection, auto-redirect sometimes causes error.
There are no cookies associated with the first request (tried
cookiecontainer also).
After I get the new URL, I do Request/Response again (no redirection this
time), response content-length is "-1" but that's acceptable regarding the
fact IE can't see the this file's size too.
when I try to download the file (Streamreader or Binarystream, makes no
difference), I get a 7 kb size file with HTML content inside of him, instead
of a 4 MB WMV file.
 
tomer said:
Hi Joerg,

I'm applying credentials and all setting again on the second request..
As for the Redirection, auto-redirect sometimes causes error.

Hm -- not that I wrote that part of the FCL, but I would be interested to
learn more about that error.
There are no cookies associated with the first request (tried
cookiecontainer also).

So you passed in an empty CookieContainer, but HttpWebResponse.Cookies came
back empty?
After I get the new URL, I do Request/Response again (no redirection
this time), response content-length is "-1" but that's acceptable
regarding the fact IE can't see the this file's size too.

Are you using HTTP 1.1 or HTTP 1.0? Returning no Content-Length is only
allowed in certain circumstances.
when I try to download the file (Streamreader or Binarystream, makes
no difference), I get a 7 kb size file with HTML content inside of
him, instead of a 4 MB WMV file.

You cannot download a binary file using StreamReader -- it's meant to read
character data. So what is the actual HTML you download? The page itself? An
error page? If you're getting HTML content (not even some binary junk), that
makes me wonder whether the download URL is correct.

Cheers,
 
Well, I've made some adjustmnet to the code,
The HTML I was gettin' was probably the LOGIN page of the mailbox.
that was after Webrequest.method = "PROPFIND",
if I change it back to "GET", I get a "500 internal server error", my guess,
'cause of authentication problems.

b.t.w - allowautoredirect = true, gives the same results (in this case).

Thanks...
 
tomer said:
Well, I've made some adjustmnet to the code,
The HTML I was gettin' was probably the LOGIN page of the mailbox.
that was after Webrequest.method = "PROPFIND",

PROPFIND is a WebDAV method. Why would you want to use that?

Cheers,
 
That was what I CHANGED, propfind was a mistake , I went back to GET.
Now I get "500 internal server error".

Any thoughts?
 
tomer said:
That was what I CHANGED, propfind was a mistake , I went back to GET.
Now I get "500 internal server error".


Any thoughts?

Use a local proxy like Fiddler (www.fiddlertool.com) to record a complete
web conversation with the web application to better understand how it works.
You obviously broke something with that GET request ;-)

Cheers,
 
Joerg,

Thanks for the tip, it turned out to be rather useful.
I managed to see what's the difference between my request and IE's request.
I turns out that IE sends the request with a cookie, which I can't seem to
find anywhere, guess it's the login cookie...
If I copy the cookie from fiddler's IE request and send my request with it,
it all goes well.
Now I only need to find a way to get this cookie.
D'you know any method of extracting cookies out of pages? am I even in the
right direction?

Thanks alot for your time,
Tomer.
 
tomer said:
Joerg,

Thanks for the tip, it turned out to be rather useful.
I managed to see what's the difference between my request and IE's
request. I turns out that IE sends the request with a cookie, which I
can't seem to find anywhere, guess it's the login cookie...
If I copy the cookie from fiddler's IE request and send my request
with it, it all goes well.
Now I only need to find a way to get this cookie.
D'you know any method of extracting cookies out of pages? am I even
in the right direction?

That's why I asked whether you're using an empty CookieContainer with your
initial request ;-)

You have to create an CookieContainer for your request, otherwise there will
be no cookies in the response:

HttpWebRequest firstRequest = (HttpWebRequest) WebRequest.Create(url);
firstRequest.CookieContainer = new CookieContainer();
HttpWebResponse firstResponse = (HttpWebResponse)
firstRequest.GetResponse();

HttpWebRequest secondRequest = (HttpWebRequest) WebRequest.Create(url);
secondRequest.CookieContainer = new CookieContainer();
CookieCollection cookies = firstResponse.Cookies;
secondRequest.CookieContainer.Add(cookies);

Cheers,
 
tomer said:
Hi Joerg,

I tried before using the cookie container.
It returned empty.
The problem is, the cookie dosen't return with the response, it has
to be SENT with the request in order to even get a proper response.

You have to receive the cookie first before you can submit it again with
another request, don't you?

You *must* use a CookieContainer with the very first request you send,
assuming that's the one that authenticates you.
I believe this cookie is stored in the cookies directories, so all I
need is to get the proper cookie for the domain from the directory,
parse the cookie file to a string and send it with the request.
Am I right?

Yes, if it is a persistent cookie. But you'll have to use Win32 APIs to read
the cookie.
See
http://msdn.microsoft.com/library/d...y/en-us/wininet/wininet/internetgetcookie.asp

Cheers,
 
Hi Joerg,

I tried before using the cookie container.
It returned empty.
The problem is, the cookie dosen't return with the response, it has to be
SENT with the request in order to even get a proper response.
I believe this cookie is stored in the cookies directories, so all I need is
to get the proper cookie for the domain from the directory, parse the cookie
file to a string and send it with the request.
Am I right?

Thanks, Tomer.
 
Hi Joerg,

The request I send is the first (because I get the download url only, from
IE after IE logged in to site).
Anyhow, the cookie is a permanent cookie so I need to read it from the
cookies directory.
As for the InternetGetCookie API, looks like it does the trick quite nicely,
dosen't look like to much hassle to wrap it into .NET.

I'm pretty confident this will solve the whole issue..
Thank you for all your help!!!

Best Regards,
Tomer.
 
Back
Top