I'm trying to load a file using webrequest method. First I need to log in then get a file or a directory listing. https:///xxx.yyy.zzz/login_template
When I look at the website source in firefox, I see
<META http-equiv="Content-Type" content="text/html">
....
<form method="post" action="/template/login" enctype="application/x-www-form- urlencoded">
....
<input name="user" type="text">
<input name="password" type="password">
<input type=hidden name="switch" value="Log In">
<input type="submit" value="Accept">
So, I wrote this code:
public static string DownloadFile()
{
CookieContainer cookieJar = new CookieContainer();
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https:///xxx.yyy.zzz/login_template");
request.CookieContainer = cookieJar;
// Set the credentials.
request.Proxy.Credentials = CredentialCache.DefaultNetworkCredentials;
request.Credentials = new NetworkCredential(userName, pass);
request.KeepAlive = true;
request.UserAgent = "SecureTransport";
request.ContentType = @"application/x-www-form-urlencoded";
request.Method = WebRequestMethods.Http.Post;
bool loggedin = false;
try
{
// first need to log in
string postData = "user=" + userName + "&Password=" + pass;
byte[] postBuffer = System.Text.Encoding.GetEncoding(1252).GetBytes(postData);
request.ContentLength = postBuffer.Length;
Stream newStream = request.GetRequestStream();
// Send the data.
newStream.Write(postBuffer, 0, postBuffer.Length);
newStream.Close();
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{
// Get the stream containing content returned by the server.
if (response.StatusCode == HttpStatusCode.OK)
{
loggedin = true;
}
response.Close();
}
The response that I get is OK - so it seems that I successfully logged in. But then, I need to go to another url to get a file https:///xxx.yyy.zzz/myfile.zip
HttpWebRequest requestToGetFile = (HttpWebRequest)WebRequest.Create("https:///xxx.yyy.zzz/myfile.zip");
requestToGetFile.Method = WebRequestMethods.Http.Get;
requestToGetFile.CookieContainer = cookieJar;
requestToGetFile.UserAgent = "SecureTransport";
requestToGetFile.ContentType = @"application/octet-stream";
using (HttpWebResponse responseToGetFile = (HttpWebResponse)requestToGetFile.GetResponse())
{
if (responseToGetDir.StatusCode != HttpStatusCode.OK)
{
...
}
}
I always get an exception System.Exception: System.Net.WebException: The remote server returned an error: (501) Not Implemented. at System.Net.HttpWebRequest.GetResponse()
I've read all I could find about this error - and it seems that the problem should be in the way I do get - there is something in the request that it's not being handled properly on the server - but I don't see what is wrong.
The "not implemented" is because you are specifying a ContentType to a GET request. It does not mean anything to the server (It is mostly used during a POST request and you want to submit a payload such as XML). You need to check the response for the right content type to make sure you are getting a zip file but to make the request, you have to remove that ContentType specification.
I think this is what MisterZimbu is pointing out in the comment as well. :)