public static Create ( System requestUri ) : System.Net.WebRequest | ||
requestUri | System | |
Результат | System.Net.WebRequest |
public Dictionary <string, string> fetchAnimeList() { HttpWebRequest req = (HttpWebRequest)WebRequest.Create("https://horriblesubs.info/shows/"); string htmlPage; using (HttpWebResponse response = (HttpWebResponse)req.GetResponse()) using (Stream stream = response.GetResponseStream()) using (StreamReader reader = new StreamReader(stream)) { htmlPage = reader.ReadToEnd(); } Dictionary <string, string> animeDictionary = new Dictionary <string, string>(); if (!String.IsNullOrEmpty(htmlPage)) { string pattern = "<div class=\"ind-show\"><a href=\"(?<url>.*?)\" title=\"(?<title>.*?)\">[\\s\\S].*?</a></div>"; MatchCollection m = Regex.Matches(htmlPage, pattern); foreach (Match match in m) { animeDictionary.Add(match.Groups["title"].Value, match.Groups["url"].Value); } } return(animeDictionary); }
private string extractShowID(string url) { HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url); string htmlPage; using (HttpWebResponse response = (HttpWebResponse)req.GetResponse()) using (Stream stream = response.GetResponseStream()) using (StreamReader reader = new StreamReader(stream)) { htmlPage = reader.ReadToEnd(); } string showID = string.Empty; if (!String.IsNullOrEmpty(htmlPage)) { // <link rel="canonical" href="https://horriblesubs.info/shows/active-raid/" /> string pattern = "<script type=\"text/javascript\">var hs_showid = (?<showID>.*?);</script>"; //string pattern = "<link rel=\"canonical\" href=\"(?<showID>.*?)\" />"; Match m = Regex.Match(htmlPage, pattern); showID = m.Groups["showID"].Value; } return(showID); }
protected virtual HttpWebRequest CreateHttpWebRequest() { var request = SystemWebRequest.Create(BuildUri()); request.Method = _method; return((HttpWebRequest)request); }
public static WebResponse GetHttpResponse(string url, string method, CookieCollection cookies = null) { Preconditions.CheckNotNull(url); Preconditions.CheckNotNull(method); if (!(WebRequest.Create(url) is HttpWebRequest request)) { throw new InvalidOperationException($"URL : {url} is invalid because request is not HttpWebRequest"); } request.Method = method; if (cookies != null) { request.CookieContainer = new CookieContainer(); request.CookieContainer.Add(cookies); } return(request.GetResponse()); }
private Models.WebResponse GetResponse(string url) { var webRequest = WebRequest.Create(url); webRequest.ContentType = "application/json; charset=utf-8"; var response = (HttpWebResponse)webRequest.GetResponse(); var dataStream = response.GetResponseStream(); if (dataStream != null) { var reader = new StreamReader(dataStream); var serverResponse = reader.ReadToEnd(); var json = JsonConvert.DeserializeObject <Models.WebResponse>(serverResponse); return(json); } return(null); }
private static WebRequestResponseData _req(string uri, HttpRequestMethod method, Dictionary <string, string> form) { var webReq = (HttpWebRequest)NetWebRequest.Create(uri); webReq.Method = method.ToString(); webReq.ContentType = "application/x-www-form-urlencoded"; var reqData = Encoding.ASCII.GetBytes(_flattenForm(form)); webReq.ContentLength = reqData.Length; using (var stream = webReq.GetRequestStream()) { stream.Write(reqData, 0, reqData.Length); } var webResponseData = new WebRequestResponseData(); using (var response = (HttpWebResponse)webReq.GetResponse()) { bool success = ((int)response.StatusCode) >= 200 && ((int)response.StatusCode) < 300; webResponseData.isSuccess = success; webResponseData.code = (int)response.StatusCode; if (success) { using (var mem = new MemoryStream()) { using (var stream = response.GetResponseStream()) { while (true) { var data = stream.ReadByte(); if (data == -1) { break; } mem.WriteByte((byte)data); } webResponseData.data = mem.ToArray(); } } } } return(webResponseData); }
public Dictionary <string, Dictionary <string, string> > GetEpisodes(string url) { string showID = string.Empty; //extract the showID, another alternative is mapping all the showID to a database dictionary, which inturn only update value when changed. showID = extractShowID(url); //append http://horriblesubs.info/lib/getshows.php?type=show&showid=284 //url = "http://horriblesubs.info/lib/getshows.php?type=show&showid=" + showID; Dictionary <String, Dictionary <string, string> > EpisodeDictionary = new Dictionary <string, Dictionary <string, string> >(); //loop each page for episode until get DONE! for (int i = 0; i >= 0; i++) { //https://horriblesubs.info/shows/active-raid/ //url = "http://horriblesubs.info//lib/getshows.php?type=show&showid=" + showID + "&nextid=" + i + "&_=" + TimeHelper.EpochConverter(DateTime.Now); url = "https://horriblesubs.info/api.php?method=getshows&type=show&showid=" + showID + "&nextid=" + i + "&_=" + TimeHelper.EpochConverter(DateTime.Now); HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url); req.Proxy = new WebProxy("127.0.0.1:8888"); string htmlPage; using (HttpWebResponse response = (HttpWebResponse)req.GetResponse()) using (Stream stream = response.GetResponseStream()) using (StreamReader reader = new StreamReader(stream)) { htmlPage = reader.ReadToEnd(); } //check if end of episode list if (htmlPage.Length == 4 && htmlPage.IndexOf("DONE") == 0) { break; } else { Dictionary <string, string> LowRes = new Dictionary <string, string>(); Dictionary <string, string> MidRes = new Dictionary <string, string>(); Dictionary <string, string> HiRes = new Dictionary <string, string>(); string linePattern = "(<div class=\"rls-info-container\" id=.*?</div></div></div>)"; MatchCollection m = Regex.Matches(htmlPage, linePattern); foreach (Match line in m) { string lineSeriesPattern = "<div class=\"rls-info-container\" id.*?</span>\\s(?<AnimeTitle>.*?) <strong>(?<Episode>.*?)</strong>"; Match lineMatch = Regex.Match(line.Value, lineSeriesPattern); string lineSeriesName = lineMatch.Groups["AnimeTitle"].Value + " - " + lineMatch.Groups["Episode"].Value; string lineMagnetPattern = "<span class=\"rls-link-label\">(?<Resolution>.*?):</span><span class=\"dl-type hs-magnet-link\"><a title=\"Magnet Link\" href=\"(?<MagnetLink>.*?)\">Magnet</a>"; MatchCollection magnetCollection = Regex.Matches(line.Value, lineMagnetPattern); foreach (Match magnetEntry in magnetCollection) { if (magnetEntry.Groups["Resolution"].Value.ToLower().Contains("480p")) { //push 480p list LowRes.Add(lineSeriesName, magnetEntry.Groups["MagnetLink"].Value); } else if (magnetEntry.Groups["Resolution"].Value.ToLower().Contains("720p")) { //push 720p list MidRes.Add(lineSeriesName, magnetEntry.Groups["MagnetLink"].Value); } else if (magnetEntry.Groups["Resolution"].Value.ToLower().Contains("1080p")) { //push 1080p list HiRes.Add(lineSeriesName, magnetEntry.Groups["MagnetLink"].Value); } } } if (LowRes.Count > 0) { if (EpisodeDictionary.ContainsKey("LowRes")) { foreach (KeyValuePair <string, string> LowResEntry in LowRes) { EpisodeDictionary["LowRes"].Add(LowResEntry.Key, LowResEntry.Value); } } else { EpisodeDictionary.Add("LowRes", LowRes); } } if (MidRes.Count > 0) { if (EpisodeDictionary.ContainsKey("MidRes")) { foreach (KeyValuePair <string, string> MidResEntry in MidRes) { EpisodeDictionary["MidRes"].Add(MidResEntry.Key, MidResEntry.Value); } } else { EpisodeDictionary.Add("MidRes", MidRes); } } if (HiRes.Count > 0) { if (EpisodeDictionary.ContainsKey("HiRes")) { foreach (KeyValuePair <string, string> HiResEntry in HiRes) { EpisodeDictionary["HiRes"].Add(HiResEntry.Key, HiResEntry.Value); } } else { EpisodeDictionary.Add("HiRes", HiRes); } } } } return(EpisodeDictionary); }
private static void _req(string uri, HttpRequestMethod method, Dictionary <string, string> form, WebRequestResponse webReqResponse) { var webReq = (HttpWebRequest)NetWebRequest.Create(uri); webReq.Method = method.ToString(); webReq.ContentType = "application/x-www-form-urlencoded"; var reqData = Encoding.UTF8.GetBytes(_flattenForm(form)); webReq.ContentLength = reqData.Length; Worker.StartWorker((out object result) => { try { if (webReq.Method != "GET") { using (var stream = webReq.GetRequestStream()) { stream.Write(reqData, 0, reqData.Length); } } var response = (HttpWebResponse)webReq.GetResponse(); var webResponseData = new WebRequestResponseData(); bool success = ((int)response.StatusCode) >= 200 && ((int)response.StatusCode) < 300; webResponseData.isSuccess = success; webResponseData.code = (int)response.StatusCode; webResponseData.data = new byte[0]; if (response != null) { using (var mem = new MemoryStream()) { using (var stream = response.GetResponseStream()) { while (true) { var data = stream.ReadByte(); if (data == -1) { break; } mem.WriteByte((byte)data); } webResponseData.data = mem.ToArray(); } } } result = webResponseData; response.Close(); } catch (WebException e) { var response = (HttpWebResponse)e.Response; var webResponseData = new WebRequestResponseData(); webResponseData.isSuccess = false; webResponseData.code = response != null ? (int)response.StatusCode : 404; webResponseData.data = new byte[0]; result = webResponseData; response?.Close(); } return(true); }, (e, result) => { if (e != null && !string.IsNullOrEmpty(e.Message)) { Console.WriteLine("WebRequest Error: " + e); } webReqResponse?.Invoke(result as WebRequestResponseData); }); }