webrequestメソッドを使用してファイルをロードしようとしています。まず、ログインしてファイルやディレクトリリストを取得する必要があります。 https:///xxx.yyy.zzz/login_templatewebrequest getは例外をスローします。「リモートサーバはエラーを返しました:(501)実装されていません。
私はFirefoxでWebサイトのソースを見てみると、私はそう
<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">
を参照してください、私はこのコードを書いた:
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();
}
私が得ることの応答がOKである - それはと思われます私は首尾よくログインしました。 しかし、私はファイルを取得するために別のURLに行く必要がありますhttps:///xxx.yyy.zzz/myfile.zip
私はいつも例外を受け取りますSystem.Exception:System.Net.WebException:リモートサーバーはエラーを返しました:(501)実装されていません。このエラーについて私が知る限りでは、私はこれを読んだことがあります - そして、私の得たやり方に問題があるように思えます - 要求にはないものがあります。サーバー上で正しく処理されていますが、何が間違っているのか分かりません。
Fiddlerを使用して実際のブラウザからのリクエストへのリクエストを比較します。 – SLaks
これはエラーを返すリモートサーバーです。そのため、WebRequestが例外をスローします。あなたはそれがリモートサーバーが好きではないものを見つける必要があります。 –
私はそれがrequestToGetFile.ContentType = "application/octet-stream"の行かもしれないと思います。もし私が間違っていないとすれば、*要求*はオクテットストリームのコンテンツタイプではなく、むしろzipファイルの応答でなければなりません。それをコメントして、それがうまくいくかどうか確認してみてください。 – MisterZimbu