Python 如何向Intersango API发出请求

Python 如何向Intersango API发出请求,python,httprequest,Python,Httprequest,我正在试图找出的正确的URL格式(这是很差的文件)。我正在用C#为我的客户机编程,但我正在查看,对于请求主体中实际放置的内容,我有点困惑: def make_request(self,call_name,params): params.append(('api_key',self.api_key)) // <-- How does this get serialized? body = urllib.urlencode(params) self.connect(

我正在试图找出的正确的URL格式(这是很差的文件)。我正在用C#为我的客户机编程,但我正在查看,对于请求主体中实际放置的内容,我有点困惑:

def make_request(self,call_name,params):
    params.append(('api_key',self.api_key)) // <-- How does this get serialized?
    body = urllib.urlencode(params) 

    self.connect()

    try:
        self.connection.putrequest('POST','/api/authenticated/v'+self.version+'/'+call_name+'.php')
        self.connection.putheader('Connection','Keep-Alive')
        self.connection.putheader('Keep-Alive','30')
        self.connection.putheader('Content-type','application/x-www-form-urlencoded')
        self.connection.putheader('Content-length',len(body))
        self.connection.endheaders()

        self.connection.send(body)

        response = self.connection.getresponse()

        return json.load(response)
//...
def make_请求(self、call_name、params):

params.append(('api_key',self.api_key))//
params
是一个包含两个元素的列表。该列表看起来像((
key1,value1),(key2,value2),…)

params.append(('api_key',self.api_key))
将另一个2元素列表添加到现有参数列表中

最后,
urllib.urlencode
获取此列表并将其转换为属性urlencoded字符串。在这种情况下,它将返回一个字符串
key1=value1&key2=value2&api_key=23423
。如果键或值中有任何特殊字符,urlencode将对其进行%编码。请参见

我试图让C#代码正常工作,但它一直失败,出现异常{“远程服务器返回错误:(417)预期失败”。}。我终于找到了问题所在。你可以深入了解它

简而言之,制作C#access Intersango API的方法是添加以下代码:

System.Net.ServicePointManager.Expect100Continue = false;
此代码只需运行一次。这是一个全局设置,因此它会影响您的完整应用程序,因此请注意,可能会导致其他内容中断

下面是一个示例代码:

System.Net.ServicePointManager.Expect100Continue = false;
var address = "https://intersango.com/api/authenticated/v0.1/listAccounts.php";
HttpWebRequest request = WebRequest.Create(address) as HttpWebRequest;
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
var postBytes = Encoding.UTF8.GetBytes("api_key=aa75***************fd65785");
request.ContentLength = postBytes.Length;
var dataStream = request.GetRequestStream();
dataStream.Write(postBytes, 0, postBytes.Length);
dataStream.Close();
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
小菜一碟 而不是
params.append(('api\u key',self.api\u key))
只要写下:

params['api_key']=self.api_key

你能做到吗?我遵循了相同的步骤,但根本无法让经过身份验证的API工作,获取{“远程服务器返回了一个错误:(417)预期失败。”}