1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
|
/*
KeePass Password Safe - The Open-Source Password Manager
Copyright (C) 2003-2025 Dominik Reichl <dominik.reichl@t-online.de>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Net;
using System.Text;
using KeePassLib.Utility;
namespace KeePass.Util
{
public static class NetUtil
{
/* public static string GZipUtf8ResultToString(DownloadDataCompletedEventArgs e)
{
if((e == null) || e.Cancelled || (e.Error != null) || (e.Result == null))
return null;
using(MemoryStream msGZ = new MemoryStream(e.Result, false))
{
using(GZipStream sGZ = new GZipStream(msGZ, CompressionMode.Decompress))
{
using(MemoryStream msUtf8 = new MemoryStream())
{
MemUtil.CopyStream(sGZ, msUtf8);
return StrUtil.Utf8.GetString(msUtf8.ToArray());
}
}
}
} */
public static string WebPageLogin(Uri uri, string strPostData,
out List<KeyValuePair<string, string>> lCookies)
{
if(uri == null) throw new ArgumentNullException("uri");
byte[] pbPostData = Encoding.ASCII.GetBytes(strPostData);
HttpWebRequest hwr = (HttpWebRequest)HttpWebRequest.Create(uri);
hwr.Method = "POST";
hwr.ContentType = "application/x-www-form-urlencoded";
hwr.ContentLength = pbPostData.Length;
hwr.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)";
using(Stream s = hwr.GetRequestStream())
{
s.Write(pbPostData, 0, pbPostData.Length);
}
string strResponse;
using(WebResponse wr = hwr.GetResponse())
{
using(Stream s = wr.GetResponseStream())
{
strResponse = MemUtil.ReadString(s, StrUtil.Utf8);
}
lCookies = new List<KeyValuePair<string, string>>();
WebHeaderCollection whc = wr.Headers;
for(int i = 0; i < whc.Count; ++i)
{
if(whc.GetKey(i) != "Set-Cookie") continue;
string strCookie = whc.Get(i);
if(strCookie == null) { Debug.Assert(false); continue; }
string[] vParts = strCookie.Split(';');
if(vParts.Length == 0) { Debug.Assert(false); continue; }
string[] vInfo = vParts[0].Split('=');
if(vInfo.Length != 2) { Debug.Assert(false); continue; }
lCookies.Add(new KeyValuePair<string, string>(vInfo[0], vInfo[1]));
}
}
return strResponse;
}
public static string WebPageGetWithCookies(Uri uri,
List<KeyValuePair<string, string>> lCookies, string strDomain)
{
if(uri == null) throw new ArgumentNullException("uri");
HttpWebRequest hwr = (HttpWebRequest)HttpWebRequest.Create(uri);
hwr.Method = "GET";
hwr.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)";
if((lCookies != null) && (lCookies.Count != 0))
{
hwr.CookieContainer = new CookieContainer();
foreach(KeyValuePair<string, string> kvp in lCookies)
{
Cookie ck = new Cookie(kvp.Key, kvp.Value, "/", strDomain);
hwr.CookieContainer.Add(ck);
}
}
using(WebResponse wr = hwr.GetResponse())
{
using(Stream s = wr.GetResponseStream())
{
return MemUtil.ReadString(s, StrUtil.Utf8);
}
}
}
}
}
|