如何使用HttpWebRequest和HttpWebResponse类来下载文件(饼干,证书等)。 [英] How to Download the File using HttpWebRequest and HttpWebResponse class(Cookies,Credentials,etc.)

查看:134
本文介绍了如何使用HttpWebRequest和HttpWebResponse类来下载文件(饼干,证书等)。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

感谢icktoofay,
我试着用的HttpWebRequest HttpWebResponse

当我通过传递像用户名和密码凭证请求的URL。

我会得到会话ID回到响应。

得到那个会话ID,如何进一步移动之后。

该身份验证的用户使用凭证/ Cookie进行追踪。
我有文件的准确网址下载和凭据。
如果你想使用cookies我会的。我需要阅读文件数据和写入/保存在指定位置。

在code我使用的是;

 字符串的用户名=;
字符串密码=;
字符串reqString =htt​​ps://xxxx.com?FileNAme=asfhasf.mro+? +
             用户名=+用户名+放大器;密码=+密码;
字节[] =的RequestData Encoding.UTF8.GetBytes(reqString);
串S1;
的CookieContainer CC =新的CookieContainer();VAR请求=(HttpWebRequest的)WebRequest.Create(loginUri);
request.Proxy = NULL;
request.CookieContainer = CC;
request.Method =POST;
HttpWebResponse WS =(HttpWebResponse)request.GetResponse();
流Str = ws.GetResponseStream();
//ws.Cookies
// VAR request1 =(HttpWebRequest的)WebRequest.Create(loginUri);
 字节[] = INBUF新的字节[100000]。
INT bytesToRead =(INT)inBuf.Length;
INT读取动作= 0;
而(bytesToRead大于0)
{
    INT N = str.Read(INBUF,读取动作,bytesToRead);
    如果(N == 0)
    打破;
    读取动作+ = N;
    bytesToRead - = N;
}
的FileStream FSTR =新的FileStream(weather.jpg,FileMode.OpenOrCreate,
                                     FileAccess.Write);
fstr.Write(INBUF,0,读取动作);
str.Close();
fstr.Close();


解决方案

这是我如何做到这一点:

 常量字符串baseURL时=HTTP://www.some......thing.com/
饼干的CookieContainer;

第一种方法登录到Web服务器并获取会话ID:

 公共方法一(字符串的用户,串密码){
  HttpWebRequest的REQ =(HttpWebRequest的)WebRequest.Create(baseURL时);  req.Method =POST;
  req.Co​​ntentType =应用/的X WWW的形式urlen codeD;
  串登录=的String.Format(走=安培;定影器= {0}&安培; Fpass = {1},用户,密码);
  字节[] = postbuf Encoding.ASCII.GetBytes(登录);
  req.Co​​ntentLength = postbuf.Length;
  流RS = req.GetRequestStream();
  rs.Write(postbuf,0,postbuf.Length);
  rs.Close();  饼干= req.Co​​okieContainer =新的CookieContainer();  WebResponse类RESP = req.GetResponse();
  resp.Close();
}

另一种方法是从服务器获取文件:

 字符串GETPAGE(字符串路径){
  HttpWebRequest的REQ =(HttpWebRequest的)WebRequest.Create(路径);
  req.Co​​okieContainer =饼干;
  WebResponse类RESP = req.GetResponse();
  串T =新的StreamReader(resp.GetResponseStream(),Encoding.Default).ReadToEnd();
  返回IsoToWin1250(T);
}

请注意,我返回页面字符串。你可能会更好的形式返回它的字节[]保存到磁盘。如果您的JPEG文件小(他们通常不是在大小千兆字节),你可以简单地把他们的内存流,然后保存到磁盘。这将需要在C#中的一些2或3个简单的线条,并与潜在危险的内存泄漏不是30线硬朗code像你上面提供。

Thanks icktoofay, I tried using HttpWebRequest and HttpWebResponse.

When I request for URL by passing the Credentials like UserName And Password.

I will get the Session Id Back in the Response.

After getting that Session Id, How to Move Further.

The authenticated user are tracked using credentials/cookies. I'm Having the Exact Url of the File to be downloaded and credentials. If you want to use Cookies I will. I need to read the File Data and write/save it in a Specified Location.

The code I'm using is;

string username = "";
string password = "";
string reqString = "https://xxxx.com?FileNAme=asfhasf.mro" + "?" + 
             "username=" + username + &password=" + password;
byte[] requestData = Encoding.UTF8.GetBytes(reqString);
string s1;
CookieContainer cc = new CookieContainer();

var request = (HttpWebRequest)WebRequest.Create(loginUri);
request.Proxy = null;
request.CookieContainer = cc;
request.Method = "POST";
HttpWebResponse ws = (HttpWebResponse)request.GetResponse();
Stream str = ws.GetResponseStream();
//ws.Cookies
//var request1 = (HttpWebRequest)WebRequest.Create(loginUri);
 byte[] inBuf = new byte[100000];
int bytesToRead = (int) inBuf.Length;
int bytesRead = 0;
while (bytesToRead > 0) 
{
    int n = str.Read(inBuf, bytesRead,bytesToRead);
    if (n==0)
    break;
    bytesRead += n;
    bytesToRead -= n;
}
FileStream fstr = new FileStream("weather.jpg", FileMode.OpenOrCreate, 
                                     FileAccess.Write);
fstr.Write(inBuf, 0, bytesRead);
str.Close();
fstr.Close();

解决方案

This is how I do it:

const string baseurl = "http://www.some......thing.com/";
CookieContainer cookie;

The first method logins to web server and gets session id:

public Method1(string user, string password) {
  HttpWebRequest req = (HttpWebRequest)WebRequest.Create(baseurl);

  req.Method = "POST";
  req.ContentType = "application/x-www-form-urlencoded";
  string login = string.Format("go=&Fuser={0}&Fpass={1}", user, password);
  byte[] postbuf = Encoding.ASCII.GetBytes(login);
  req.ContentLength = postbuf.Length;
  Stream rs = req.GetRequestStream();
  rs.Write(postbuf,0,postbuf.Length);
  rs.Close();

  cookie = req.CookieContainer = new CookieContainer();

  WebResponse resp = req.GetResponse();
  resp.Close();
}

The other method gets the file from server:

string GetPage(string path) {
  HttpWebRequest req = (HttpWebRequest)WebRequest.Create(path);
  req.CookieContainer = cookie;
  WebResponse resp = req.GetResponse();
  string t = new StreamReader(resp.GetResponseStream(), Encoding.Default).ReadToEnd();
  return IsoToWin1250(t);
}

Note that I return the page as string. You would probably better return it as bytes[] to save to disk. If your jpeg files are small (they usually are not gigabyte in size), you can simply put them to memory stream, and then save to disk. It will take some 2 or 3 simple lines in C#, and not 30 lines of tough code with potential dangerous memory leaks like you provided above.

这篇关于如何使用HttpWebRequest和HttpWebResponse类来下载文件(饼干,证书等)。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆