如何在Python中像C# HttpClient一样执行HTTP Put请求
我有以下 C# 代码,可以完美地验证用户身份(响应 200):
using (var client = new HttpClient(new HttpClientHandler { UseDefaultCredentials = true }))
{
HttpResponseMessage response = client.PutAsync(url,content).GetAwaiter().GetResult();
}
我想在 Python 中本地执行相同的操作,尽管异步调用对我来说并不重要。我已经尝试过了:
import requests
headers = {'Content-type': 'application/json'}
r = requests.put(my_url,my_unicode_object,headers=headers,verify=False,auth=(username,pass))
但是我收到了 401 未经授权的响应。我有所有 C# 变量可以并排比较,但我仍然不知道如何在 Python 中实现相同的工作。
更新: 在 VS 中,当我输入“System.Net.CredentialCache.DefaultNetworkCredentials”时,我得到以下信息:
Domain: ""
Password: ""
SecurePassword: {System.Security.SecureString}
UserName: ""
我不认为它实际上使用任何凭据,但我尝试不使用 auth 或 auth = ('','') http put request 仍然给出 401 请求响应。
I have the following code in C# that works perfectly at authenticating the user (reponse 200):
using (var client = new HttpClient(new HttpClientHandler { UseDefaultCredentials = true }))
{
HttpResponseMessage response = client.PutAsync(url,content).GetAwaiter().GetResult();
}
I want to do the same thing natively in Python, though the asynchronous call isn't important to me. I've tried:
import requests
headers = {'Content-type': 'application/json'}
r = requests.put(my_url,my_unicode_object,headers=headers,verify=False,auth=(username,pass))
However I get a 401 unauthorized response. I have all my C# variables to compare side-by-side and I still don't know how to get the same working in Python.
Update:
In VS when I type "System.Net.CredentialCache.DefaultNetworkCredentials" I get the following:
Domain: ""
Password: ""
SecurePassword: {System.Security.SecureString}
UserName: ""
I don't think it's actually using any credentials, but with I try not using auth or auth = ('','') the http put request still gives a 401 request response.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论