def _request_ironython(self, *args, **kwargs): method, url = args headers = kwargs.get("headers") params = kwargs.get("params") json_data = kwargs.get("json_data") byte_data = kwargs.get("byte_data") urlencode = kwargs.get("urlencode") filepath = kwargs.get("filepath") try: # prepare params if params: url = self._add_url_params(url, params) web_request = WebRequest.Create(url) web_request.Method = method.upper() web_request.Timeout = self.timeout # prepare headers if headers: for key, value in headers.items(): if key == "Content-Type": web_request.ContentType = value elif key == "Content-Length": web_request.ContentLength = value else: web_request.Headers.Add(key, value) byte_arrays = [] if json_data: byte_arrays.append( UTF8.GetBytes(json.dumps(json_data, ensure_ascii=False))) if filepath: byte_arrays.append(File.ReadAllBytes(filepath)) if byte_data: pass # TODO - Add byte input for System.Net if urlencode: byte_arrays.append(UTF8.GetBytes(self._url_encode(urlencode))) for byte_array in byte_arrays: web_request.ContentLength = byte_array.Length with web_request.GetRequestStream() as req_stream: req_stream.Write(byte_array, 0, byte_array.Length) try: with web_request.GetResponse() as response: success = response.StatusDescription in SUCCESS_CODES with response.GetResponseStream() as response_stream: with StreamReader(response_stream) as stream_reader: data = json.loads(stream_reader.ReadToEnd()) except SystemError: return None, None finally: web_request.Abort() except Exception as e: raise e return data, success
def onUpdate(): shortenedUri = None if NetworkInterface.GetIsNetworkAvailable(): try: request = WebRequest.Create(Uri(String.Concat("http://nazr.in/api/shorten.json?url=", urlEncode(uri.ToString())))) response = None stream = None streamReader = None try: response = request.GetResponse() stream = response.GetResponseStream() streamReader = StreamReader(stream) jsonDictionary = JsonDecoder.decode(streamReader.ReadToEnd()) if jsonDictionary is not None and clr.GetClrType(Dictionary[String, Object]).IsInstanceOfType(jsonDictionary) and jsonDictionary.ContainsKey("url"): shortenedUri = Uri(jsonDictionary["url"]) finally: if streamReader is not None: streamReader.Close() if stream is not None: stream.Close() if response is not None: response.Close() except Exception, e: Trace.WriteLine(e.clsException.Message) Trace.WriteLine(e.clsException.StackTrace)
def get_html_string(url): ''' This method takes a url (string) of a webpage, and connects to the URL, then downloads, htmldecodes, and returns the contents of that page as an html string. This method will throw an WebException or IOException if anything goes wrong, including if the response code is not valid (i.e. 200). ''' try: ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 request = WebRequest.Create(url) request.UserAgent = "[ComicVineScraper, version " + \ Resources.SCRIPT_VERSION + "]" response = request.GetResponse() # if the response code is not "OK", throw a web exception immediately. # this stops red-herring errors later on as we try to parse bad results. # usually this only happens if the CV server is temporarily down. if response.StatusCode != HttpStatusCode.OK: raise WebException("server response code " + sstr(int(response.StatusCode)) + " (" + sstr(response.StatusCode) + ")") responseStream = response.GetResponseStream() reader = StreamReader(responseStream, Encoding.UTF8) page = reader.ReadToEnd() with StringWriter() as writer: HttpUtility.HtmlDecode(page, writer) page = writer.ToString() return page finally: if 'reader' in vars(): reader.Close() if 'responseStream' in vars(): responseStream.Close() if 'response' in vars(): response.Close()
def getDocument(listUrl, credentials): request = WebRequest.Create(listUrl) request.Method = "GET" request.Credentials = credentials with request.GetResponse() as response: with response.GetResponseStream() as stream: return XDocument.Load(stream)
def post(self, url, payload='', json=None): r = WebRequest.Create(url) r.Method = "POST" #r.Accept = "application/json" if self.proxy_aware: r.Proxy = WebRequest.GetSystemWebProxy() r.Proxy.Credentials = CredentialCache.DefaultCredentials if json: r.ContentType = "application/json" if type(json) == dict: payload = JavaScriptSerializer().Serialize(json) elif hasattr(json, '__serialize__'): payload = JavaScriptSerializer().Serialize( json.__serialize__()) else: raise NotSerializable("{} object is not serializable".format( type(json))) if len(payload): data = Encoding.ASCII.GetBytes(payload) r.ContentLength = data.Length requestStream = r.GetRequestStream() requestStream.Write(data, 0, data.Length) requestStream.Close() response = r.GetResponse() responseStream = StreamReader(response.GetResponseStream()) return Response(responseStream.ReadToEnd())
def get(self, url): r = WebRequest.Create(url) r.ContentType = "application/octet-stream" r.Method = "GET" if self.proxy_aware: r.Proxy = WebRequest.GetSystemWebProxy() r.Proxy.Credentials = CredentialCache.DefaultCredentials return Response(r.GetResponse())
def PostData(jsonBody): request = WebRequest.Create(API_SERVER_URI) request.ContentType = "application/json" request.Method = "POST" bytes = Encoding.ASCII.GetBytes(jsonBody) request.ContentLength = bytes.Length reqStream = request.GetRequestStream() reqStream.Write(bytes, 0, bytes.Length) reqStream.Close()
def can_access(url_to_open): try: client = WebRequest.Create(url_to_open) client.Method = "HEAD" client.Timeout = timeout response = client.GetResponse() response.GetResponseStream() return True except: return False
def update(): global username, password request = WebRequest.Create("https://mail.google.com/mail/feed/atom") request.Credentials = NetworkCredential(username, password) entryList = List[Entry]() def onUpdate(): if NetworkInterface.GetIsNetworkAvailable(): try: response = None stream = None try: response = request.GetResponse() stream = response.GetResponseStream() doc = XmlDocument() doc.Load(stream) for entryXmlNode in doc.GetElementsByTagName("entry"): entry = Entry() for xmlNode in entryXmlNode.ChildNodes: if xmlNode.Name.Equals("title"): entry.Title = xmlNode.InnerText elif xmlNode.Name.Equals("issued"): entry.Created = DateTime.Parse( xmlNode.InnerText) elif xmlNode.Name.Equals("modified"): entry.Modified = DateTime.Parse( xmlNode.InnerText) elif xmlNode.Name.Equals("link"): for attribute in xmlNode.Attributes: if attribute.Name.Equals("href"): entry.Resource = Uri(attribute.Value) elif xmlNode.Name.Equals("author"): for childXmlNode in xmlNode.ChildNodes: if childXmlNode.Name.Equals("name"): entry.Author = childXmlNode.InnerText entry.Image = Uri( "http://www.google.co.jp/options/icons/gmail.gif") entryList.Add(entry) finally: if stream is not None: stream.Close() if response is not None: response.Close() except Exception, e: Trace.WriteLine(e.clsException.Message) Trace.WriteLine(e.clsException.StackTrace)
def get(self, url): r = WebRequest.Create(url) r.Method = "GET" if self.proxy_aware: r.Proxy = WebRequest.GetSystemWebProxy() r.Proxy.Credentials = CredentialCache.DefaultCredentials #r.ContentType = "application/json" #r.Accept = "application/json" response = r.GetResponse() responseStream = StreamReader(response.GetResponseStream()) return Response(responseStream.ReadToEnd())
def rsnrequest(): request = WebRequest.Create(file_link) request.Method = "GET" request.UserAgent = "Anything" # Token scenario for closed repositories #token = '' #request.Headers["OAUTH-TOKEN"] = token rsp = request.GetResponse() stream_reader = StreamReader(rsp.GetResponseStream()) jsonData = stream_reader.ReadToEnd() stream_reader.Close() json = JavaScriptSerializer().DeserializeObject(jsonData) return json
def FetchURIWithParameters(uri, parameters=None): request = WebRequest.Create(uri) if parameters is not None: request.ContentType = "application/x-www-form-urlencoded" request.Method = "POST" bytes = Encoding.ASCII.GetBytes(parameters) request.ContentLength = bytes.Length reqStream = request.GetRequestStream() reqStream.Write(bytes, 0, bytes.Length) reqStream.Close() response = request.GetResponse() result = StreamReader(response.GetResponseStream()).ReadToEnd() return result
def UrlOpen(uri, parameters=None): request = WebRequest.Create(uri) if parameters is not None: #request.ContentType = "application/x-www-form-urlencoded" request.ContentType = "text/plain;charset=utf-8" request.Method = "POST" bytes = Encoding.UTF8.GetBytes(parameters) #ASCII request.ContentLength = bytes.Length reqStream = request.GetRequestStream() reqStream.Write(bytes, 0, bytes.Length) reqStream.Close() response = request.GetResponse() result = StreamReader(response.GetResponseStream()).ReadToEnd() return result
def webhook(msg): URI = 'https://discordapp.com/api/webhooks/654131394854781018/KySfeUnLjoFmqfmf154_1V22nxkzGnDmOP6-j8Kj__Ei8boMeHwzir6irC57rIY65_6q' alert = msg report = "content=" + alert PARAMETERS = report from System.Net import WebRequest request = WebRequest.Create(URI) request.ContentType = "application/x-www-form-urlencoded" request.Method = "POST" from System.Text import Encoding bytes = Encoding.ASCII.GetBytes(PARAMETERS) request.ContentLength = bytes.Length reqStream = request.GetRequestStream() reqStream.Write(bytes, 0, bytes.Length) reqStream.Close() response = request.GetResponse() from System.IO import StreamReader result = StreamReader(response.GetResponseStream()).ReadToEnd().replace( '\r', '\n')
def post(self, url, payload=None): r = WebRequest.Create(url) r.ContentType = "application/octet-stream" r.Method = "POST" if self.proxy_aware: r.Proxy = WebRequest.GetSystemWebProxy() r.Proxy.Credentials = CredentialCache.DefaultCredentials if len(payload): if type(payload) != Array[Byte]: data = Encoding.UTF8.GetBytes(payload) else: data = payload r.ContentLength = data.Length requestStream = r.GetRequestStream() requestStream.Write(data, 0, data.Length) requestStream.Close() return Response(r.GetResponse())
def _query_image( ref, lasttry = False ): ''' ComicVine implementation of the identically named method in the db.py ''' retval = None # the Image object that we will return # 1. determine the URL image_url_s = None if isinstance(ref, SeriesRef): image_url_s = ref.thumb_url_s elif isinstance(ref, IssueRef): image_url_s = ref.thumb_url_s elif is_string(ref): image_url_s = ref # 2. attempt to load the image for the URL if image_url_s: response = None response_stream = None try: cvconnection.wait_until_ready() # throttle our request speed request = WebRequest.Create(image_url_s) request.UserAgent = "[ComicVineScraper, version " + \ Resources.SCRIPT_VERSION + "]" response = request.GetResponse() response_stream = response.GetResponseStream() retval = Image.FromStream(response_stream) except: if lasttry: log.debug_exc('ERROR retry image load failed:') retval = None else: log.debug('RETRY loading image -> ', image_url_s) retval = _query_image( ref, True ) finally: if response: response.Dispose() if response_stream: response_stream.Dispose() # if this value is stil None, it means an error occurred, or else comicvine # simply doesn't have any Image for the given ref object return retval
def send_request_ironpythonlegacy(uri, parameters): """ IronPython 2.7.3 and earlier has a built in problem with urlib2 library when verifying certificates. This code calls a .NET library instead. """ from System.Net import WebRequest from System.IO import StreamReader from System.Text import Encoding request = WebRequest.Create(uri) request.ContentType = "application/x-www-form-urlencoded" request.Method = "POST" #work for post bytes = Encoding.ASCII.GetBytes(parameters) request.ContentLength = bytes.Length reqStream = request.GetRequestStream() reqStream.Write(bytes, 0, bytes.Length) reqStream.Close() response = request.GetResponse() result = StreamReader(response.GetResponseStream()).ReadToEnd() return result
def get_html_string(url): ''' This method takes a url (string) of a webpage, and connects to the URL, then downloads, htmldecodes, and returns the contents of that page as an html string. This method will throw an WebException or IOException if anything goes wrong, including if the response code is not valid (i.e. 200). ''' try: ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 request = WebRequest.Create(url) # latest version of comicvine api insists on a user agent (see bug #484). # previously, it didn't like my non-standard user agent, I think (see bug #471). # now, I have a standard user agent, which comicvine api seems to like. request.UserAgent = "ComicVineScraper/" + \ Resources.SCRIPT_VERSION + " (https://github.com/cbanack/comic-vine-scraper/)" response = request.GetResponse() # if the response code is not "OK", throw a web exception immediately. # this stops red-herring errors later on as we try to parse bad results. # usually this only happens if the CV server is temporarily down. if response.StatusCode != HttpStatusCode.OK: raise WebException("server response code " + sstr(int(response.StatusCode))+" ("+sstr(response.StatusCode)+")" ) responseStream = response.GetResponseStream() reader = StreamReader(responseStream, Encoding.UTF8) page = reader.ReadToEnd() with StringWriter() as writer: HttpUtility.HtmlDecode(page, writer) page = writer.ToString() return page finally: if 'reader' in vars(): reader.Close() if 'responseStream' in vars(): responseStream.Close() if 'response' in vars(): response.Close()
urlList = [] for i,j,k,z in zip(TileColumns2,TileRows2,Layers,Zoomlevels): a = [] for l,m in zip(i,j): b = string1 + str(k) + string3 + str(z) + string34 + str(m) + string5 + str(l) + string7 a.append(b) urlList.append(a) bitmaps2 = [] for i in urlList: bitmaps = [] for j in i: request = WebRequest.Create(j) request.Accept = "text/html" request.UserAgent = "Mozilla/5.0" response = request.GetResponse() bitmaps.append(Image.FromStream(response.GetResponseStream())) bitmaps2.append(bitmaps) combinedBitmaps = [] for a,b,c in zip(bitmaps2,UniqueTileColumns,UniqueTileRows): TotalWidth = len(b)*PixelWidth TotalHeight = len(c)*PixelWidth img = Bitmap(TotalWidth,TotalHeight) g = Graphics.FromImage(img) LPx = [] n = 0 for l in j:
def onUpdate(): temp = 0 windSpeed = 0 windDeg = 0 weatherIdList = List[Double]() weatherPathHashSet = HashSet[String]() weatherStreamList = List[MemoryStream]() weatherConditionList = List[String]() if NetworkInterface.GetIsNetworkAvailable(): try: request = WebRequest.Create(Uri(String.Concat("http://api.openweathermap.org/data/2.5/find?q=", urlEncode(location), "&units=metric&cnt=1"))) response = None stream = None streamReader = None try: nowDateTime = DateTime.Now response = request.GetResponse() stream = response.GetResponseStream() streamReader = StreamReader(stream) jsonDictionary = JsonDecoder.decode(streamReader.ReadToEnd()) if jsonDictionary is not None and clr.GetClrType(Dictionary[String, Object]).IsInstanceOfType(jsonDictionary) and jsonDictionary.ContainsKey("list") and jsonDictionary["list"] is not None and clr.GetClrType(Array).IsInstanceOfType(jsonDictionary["list"]): for obj in jsonDictionary["list"]: if obj is not None and clr.GetClrType(Dictionary[String, Object]).IsInstanceOfType(obj): if obj.ContainsKey("main") and obj["main"] is not None and clr.GetClrType(Dictionary[String, Object]).IsInstanceOfType(obj["main"]) and obj["main"].ContainsKey("temp"): temp = obj["main"]["temp"] if obj.ContainsKey("wind") and obj["wind"] is not None and clr.GetClrType(Dictionary[String, Object]).IsInstanceOfType(obj["wind"]): if obj["wind"].ContainsKey("speed"): windSpeed = obj["wind"]["speed"] if obj["wind"].ContainsKey("deg"): windDeg = obj["wind"]["deg"] if obj.ContainsKey("weather") and obj["weather"] is not None and clr.GetClrType(Array).IsInstanceOfType(obj["weather"]): for o in obj["weather"]: if o is not None and clr.GetClrType(Dictionary[String, Object]).IsInstanceOfType(o) and o.ContainsKey("id") and o["id"] is not None: weatherIdList.Add(o["id"]) for id in weatherIdList: digit = Convert.ToInt32(id / 100) path = None s = None if digit == 2: path = "Assets\\Cloud-Lightning.png" weatherConditionList.Add("Thunderstorm") elif digit == 3: path = "Assets\\Cloud-Drizzle.png" weatherConditionList.Add("Drizzle") elif digit == 5: d = Convert.ToInt32(id / 10) if d == 0: if nowDateTime.Hour > 6 and nowDateTime.Hour <= 18: path = "Assets\\Cloud-Rain-Sun.png" else: path = "Assets\\Cloud-Rain-Moon.png" else: path = "Assets\\Cloud-Rain.png" weatherConditionList.Add("Rain") elif digit == 6: path = "Assets\\Cloud-Snow.png" weatherConditionList.Add("Snow") elif digit == 7: path = "Assets\\Cloud-Fog.png" if Convert.ToInt32(id) == 701: weatherConditionList.Add("Mist") elif Convert.ToInt32(id) == 711: weatherConditionList.Add("Smoke") elif Convert.ToInt32(id) == 721: weatherConditionList.Add("Haze") elif Convert.ToInt32(id) == 731: weatherConditionList.Add("Dust") elif Convert.ToInt32(id) == 741: weatherConditionList.Add("Fog") elif digit == 8: if Convert.ToInt32(id) == 800: if nowDateTime.Hour > 6 and nowDateTime.Hour <= 18: path = "Assets\\Sun.png" weatherConditionList.Add("Sunny") else: path = "Assets\\Moon.png" weatherConditionList.Add("Clear") elif Convert.ToInt32(id) >= 801 and Convert.ToInt32(id) <= 803: if nowDateTime.Hour > 6 and nowDateTime.Hour <= 18: path = "Assets\\Cloud-Sun.png" else: path = "Assets\\Cloud-Moon.png" weatherConditionList.Add("Cloudy") elif Convert.ToInt32(id) == 804: path = "Assets\\Cloud.png" weatherConditionList.Add("Overcast") else: if Convert.ToInt32(id) == 900: path = "Assets\\Tornado.png" weatherConditionList.Add("Tornado") elif Convert.ToInt32(id) == 905: path = "Assets\\Wind.png" weatherConditionList.Add("Windy") elif Convert.ToInt32(id) == 906: path = "Assets\\Cloud-Hail.png" weatherConditionList.Add("Hail") if path is not None and weatherPathHashSet.Contains(path) == False: fs = None try: fs = FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read) ms = MemoryStream() buffer = Array.CreateInstance(Byte, fs.Length) bytesRead = fs.Read(buffer, 0, buffer.Length) while bytesRead > 0: ms.Write(buffer, 0, bytesRead) bytesRead = fs.Read(buffer, 0, buffer.Length) ms.Seek(0, SeekOrigin.Begin) weatherStreamList.Add(ms) finally: if fs is not None: fs.Close() weatherPathHashSet.Add(path) finally: if streamReader is not None: streamReader.Close() if stream is not None: stream.Close() if response is not None: response.Close() except Exception, e: Trace.WriteLine(e.clsException.Message) Trace.WriteLine(e.clsException.StackTrace)
def update(): request = WebRequest.Create( "http://tenki.jp/component/static_api/rss/earthquake/recent_entries_by_day.xml" ) entryList = List[Entry]() def onUpdate(): if NetworkInterface.GetIsNetworkAvailable(): try: response = None stream = None try: response = request.GetResponse() stream = response.GetResponseStream() doc = XmlDocument() doc.Load(stream) for itemXmlNode in doc.GetElementsByTagName("item"): entry = Entry() epicenter = None maxLevel = None for xmlNode in itemXmlNode.ChildNodes: if xmlNode.Name.Equals("link"): entry.Resource = Uri(xmlNode.InnerText) elif xmlNode.Name.Equals("description"): entry.Description = xmlNode.InnerText elif xmlNode.Name.Equals("tenkiJP:earthquake"): for attribute in xmlNode.Attributes: if attribute.Name.Equals("epicenter"): epicenter = attribute.Value elif attribute.Name.Equals("max_level"): maxLevel = attribute.Value elif attribute.Name.Equals( "outbreak_datetime"): entry.Created = entry.Modified = DateTime.Parse( attribute.Value) if epicenter is not None: if String.IsNullOrEmpty(maxLevel): maxLevel = "N/A" if CultureInfo.CurrentCulture.Equals( CultureInfo.GetCultureInfo("ja-JP")): entry.Title = String.Format( "震度{0} - {1}", maxLevel, epicenter) else: entry.Title = String.Format( "Intensity {0} - {1}", maxLevel, epicenter) entryList.Add(entry) finally: if stream is not None: stream.Close() if response is not None: response.Close() except Exception, e: Trace.WriteLine(e.clsException.Message) Trace.WriteLine(e.clsException.StackTrace)
def web_request(obj, uri_string, func): req = WebRequest.Create(Uri(uri_string)) req.BeginGetResponse(AsyncCallback(web_complete(obj, func)), req)
def HTTPRequest(txt): request = WebRequest.Create(TelegramApi.Uri + txt) response = request.GetResponse() return StreamReader(response.GetResponseStream()).ReadToEnd()