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