2323class HelperMethods :
2424
2525 server_address = "https://app.cryptolens.io/api/"
26+ ironpython2730_legacy = False
2627
2728 @staticmethod
2829 def get_SHA256 (string ):
@@ -118,10 +119,41 @@ def send_request(method, params):
118119
119120 method: the path of the method, eg. key/activate
120121 params: a dictionary of parameters
121- """
122- return urllib2 .urlopen (HelperMethods .server_address + method , \
123- urllib .urlencode (params )).read ().decode ("utf-8" )
124-
122+ """
123+
124+ if HelperMethods .ironpython2730_legacy :
125+ return HelperMethods .send_request_ironpythonlegacy (HelperMethods .server_address + method , \
126+ urllib .urlencode (params ))
127+ else :
128+ return urllib2 .urlopen (HelperMethods .server_address + method , \
129+ urllib .urlencode (params )).read ().decode ("utf-8" )
130+
131+ @staticmethod
132+ def send_request_ironpythonlegacy (uri , parameters ):
133+ """
134+ IronPython 2.7.3 and earlier has a built in problem with
135+ urlib2 library when verifying certificates. This code calls a .NET
136+ library instead.
137+ """
138+ from System .Net import WebRequest
139+ from System .IO import StreamReader
140+ from System .Text import Encoding
141+
142+ request = WebRequest .Create (uri )
143+
144+ request .ContentType = "application/x-www-form-urlencoded"
145+ request .Method = "POST" #work for post
146+ bytes = Encoding .ASCII .GetBytes (parameters )
147+ request .ContentLength = bytes .Length
148+ reqStream = request .GetRequestStream ()
149+ reqStream .Write (bytes , 0 , bytes .Length )
150+ reqStream .Close ()
151+
152+ response = request .GetResponse ()
153+ result = StreamReader (response .GetResponseStream ()).ReadToEnd ()
154+ return result
155+
156+
125157 @staticmethod
126158 def start_process (command ):
127159
@@ -187,8 +219,8 @@ def activate(token, rsa_pub_key, product_id, key, machine_code, fields_to_return
187219
188220 response = Response ("" ,"" ,0 ,"" )
189221
190- try :
191- response = Response .from_string (HelperMethods .send_request ("key/activate" , {"token" :token ,\
222+ # try:
223+ response = Response .from_string (HelperMethods .send_request ("key/activate" , {"token" :token ,\
192224 "ProductId" :product_id ,\
193225 "key" :key ,\
194226 "MachineCode" :machine_code ,\
@@ -198,8 +230,8 @@ def activate(token, rsa_pub_key, product_id, key, machine_code, fields_to_return
198230 "MaxOverdraft" : max_overdraft ,\
199231 "Sign" :"True" ,\
200232 "SignMethod" :1 }))
201- except Exception :
202- return (None , "Could not contact the server." )
233+ # except Exception:
234+ # return (None, "Could not contact the server.")
203235
204236 pubkey = RSAPublicKey .from_string (rsa_pub_key )
205237
0 commit comments