将 python 个 http 代码转换为 C#
Convert python http code to C#
我在 python
中有这段代码
response = requests.post(
"https://gateway.watsonplatform.net/personality-insights/api/v2/profile",
auth = ("username", "password"),
headers = {"content-type": "text/plain"},
data = "your text goes here"
)
jsonProfile = json.loads(response.text)
我正在尝试将其转换为 C#,下面是我的代码:
public void getRequest() {
string url = "https://gateway.watsonplatform.net/personality-insights/api/v2/profile";
using (var client = new WebClient())
{
var values = new NameValueCollection();
values["username"] = username;
values["password"] = password;
values["content-type"] = "text/plain";
values["data"] = getTestWords(@"D:\Userfiles\tchaiyaphan\Documents\API and Intelligence\storyTestWord.txt");
var response = client.UploadValues(url, values);
var responseString = Encoding.Default.GetString(response);
}
}
我不知道如何处理页眉部分,所以我把它漏掉了。当我 运行 代码时,它给了我一个 401 错误。我不知道该怎么办!
问题是您的代码将用户名和密码作为 POST 数据发送,而不是使用正确的 HTTP 授权 header。
client.Credentials = new NetworkCredential(username, password);
虽然 ThiefMaster 设法让我通过了身份验证,但这次它给了我一个不同的错误(415 Un-supported media type)所以我决定采用不同的方法并且它有效。
var request = (HttpWebRequest)WebRequest.Create("https://gateway.watsonplatform.net/personality-insights/api/v2/profile");
var postData = getTestWords(@"D:\Userfiles\tchaiyaphan\Documents\API and Intelligence\storyTestWord.txt");
var data = Encoding.ASCII.GetBytes(postData);
request.Method = "POST";
request.ContentType = "text/plain";
request.ContentLength = data.Length;
request.Credentials = new NetworkCredential(username, password);
using (var stream = request.GetRequestStream())
{
stream.Write(data, 0, data.Length);
}
var response = (HttpWebResponse)request.GetResponse();
var responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();
我在 python
中有这段代码response = requests.post(
"https://gateway.watsonplatform.net/personality-insights/api/v2/profile",
auth = ("username", "password"),
headers = {"content-type": "text/plain"},
data = "your text goes here"
)
jsonProfile = json.loads(response.text)
我正在尝试将其转换为 C#,下面是我的代码:
public void getRequest() {
string url = "https://gateway.watsonplatform.net/personality-insights/api/v2/profile";
using (var client = new WebClient())
{
var values = new NameValueCollection();
values["username"] = username;
values["password"] = password;
values["content-type"] = "text/plain";
values["data"] = getTestWords(@"D:\Userfiles\tchaiyaphan\Documents\API and Intelligence\storyTestWord.txt");
var response = client.UploadValues(url, values);
var responseString = Encoding.Default.GetString(response);
}
}
我不知道如何处理页眉部分,所以我把它漏掉了。当我 运行 代码时,它给了我一个 401 错误。我不知道该怎么办!
问题是您的代码将用户名和密码作为 POST 数据发送,而不是使用正确的 HTTP 授权 header。
client.Credentials = new NetworkCredential(username, password);
虽然 ThiefMaster 设法让我通过了身份验证,但这次它给了我一个不同的错误(415 Un-supported media type)所以我决定采用不同的方法并且它有效。
var request = (HttpWebRequest)WebRequest.Create("https://gateway.watsonplatform.net/personality-insights/api/v2/profile");
var postData = getTestWords(@"D:\Userfiles\tchaiyaphan\Documents\API and Intelligence\storyTestWord.txt");
var data = Encoding.ASCII.GetBytes(postData);
request.Method = "POST";
request.ContentType = "text/plain";
request.ContentLength = data.Length;
request.Credentials = new NetworkCredential(username, password);
using (var stream = request.GetRequestStream())
{
stream.Write(data, 0, data.Length);
}
var response = (HttpWebResponse)request.GetResponse();
var responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();