HttpWebResponse: The remote server returned an error: (400) Bad Request
HttpWebResponse: The remote server returned an error: (400) Bad Request
我正在尝试 post 编写我的 gist 帐户代码。如果我写一行代码,它就可以工作。但是如果我写多行代码,我会得到错误。
例如 运行 [1]:
Console.WriteLine();
例如不工作[2]:
python main.py
>>> Programming Quotes
UNIX was not designed to stop its users from doing stupid things, as that would
also stop them from doing clever things. - Doug Gwyn
python main.py
>>> Programming Quotes
And folks, let's be honest. Sturgeon was an optimist. Way more than 90% of code
is crap. - viro
我无法post 第二个示例中的代码。我收到“远程服务器返回错误:(400) 错误请求。”错误。
也许你需要知道
textBox1.Text 用于要点描述
richTextBox1.Text 用于内容正文 --> 不适用于多行 post 数据
我的代码如下:
String jsonMessage = "{ \"description\": \"" +textBox1.Text + "\", \"public\": true,"
+ "\"files\": { \"file1.cs\": {"
+ "\"content\":\""+ richTextBox1.Text.ToString() + "\" } }}";
String _url = "https://api.github.com/gists";
HttpWebRequest req = WebRequest.Create(new Uri(_url)) as HttpWebRequest;
String userName = "GITHUB_USERNAME";
String userPassword = "GITHUB_PASSWORD";
string authInfo = userName + ":" + userPassword;
authInfo = Convert.ToBase64String(Encoding.Default.GetBytes(authInfo));
req.Method = "POST";
req.ContentType = "application/json";
req.Headers.Add("Authorization", "Basic " + authInfo);
req.UserAgent = "Test-Agent";
StreamWriter writer = new StreamWriter(req.GetRequestStream());
//MessageBox.Show(jsonMessage);
writer.Write(jsonMessage);
writer.Close();
string result = null;
using (HttpWebResponse resp = req.GetResponse() as HttpWebResponse) // Error Line
{
StreamReader reader = new StreamReader(resp.GetResponseStream());
result = reader.ReadToEnd();
var jo = JObject.Parse(result);
//MessageBox.Show(jo["url"].ToString());
textBox2.Text = "https://gist.github.com/" + jo["id"].ToString();
}
我该如何解决这个问题?我该怎么办?
谢谢!
richTextBox1.Text for content body --> not working for multiline post datas
新行字符应该在 json 字符串中转义...因为您使用字符串操作形成 json,所以您会得到一个包含多行内容的无效 json...
你可以轻松测试它here。
创建 json 字符串的正确方法是使用 json 序列化程序。由于您已经使用了 Json.Net,您可以像
var json = JsonConvert.SerializeObject(
new
{
description = textBox1.Text,
content = richTextBox1.Text,
@public = true
}
);
我正在尝试 post 编写我的 gist 帐户代码。如果我写一行代码,它就可以工作。但是如果我写多行代码,我会得到错误。
例如 运行 [1]:
Console.WriteLine();
例如不工作[2]:
python main.py
>>> Programming Quotes
UNIX was not designed to stop its users from doing stupid things, as that would
also stop them from doing clever things. - Doug Gwyn
python main.py
>>> Programming Quotes
And folks, let's be honest. Sturgeon was an optimist. Way more than 90% of code
is crap. - viro
我无法post 第二个示例中的代码。我收到“远程服务器返回错误:(400) 错误请求。”错误。
也许你需要知道
textBox1.Text 用于要点描述
richTextBox1.Text 用于内容正文 --> 不适用于多行 post 数据
我的代码如下:
String jsonMessage = "{ \"description\": \"" +textBox1.Text + "\", \"public\": true,"
+ "\"files\": { \"file1.cs\": {"
+ "\"content\":\""+ richTextBox1.Text.ToString() + "\" } }}";
String _url = "https://api.github.com/gists";
HttpWebRequest req = WebRequest.Create(new Uri(_url)) as HttpWebRequest;
String userName = "GITHUB_USERNAME";
String userPassword = "GITHUB_PASSWORD";
string authInfo = userName + ":" + userPassword;
authInfo = Convert.ToBase64String(Encoding.Default.GetBytes(authInfo));
req.Method = "POST";
req.ContentType = "application/json";
req.Headers.Add("Authorization", "Basic " + authInfo);
req.UserAgent = "Test-Agent";
StreamWriter writer = new StreamWriter(req.GetRequestStream());
//MessageBox.Show(jsonMessage);
writer.Write(jsonMessage);
writer.Close();
string result = null;
using (HttpWebResponse resp = req.GetResponse() as HttpWebResponse) // Error Line
{
StreamReader reader = new StreamReader(resp.GetResponseStream());
result = reader.ReadToEnd();
var jo = JObject.Parse(result);
//MessageBox.Show(jo["url"].ToString());
textBox2.Text = "https://gist.github.com/" + jo["id"].ToString();
}
我该如何解决这个问题?我该怎么办?
谢谢!
richTextBox1.Text for content body --> not working for multiline post datas
新行字符应该在 json 字符串中转义...因为您使用字符串操作形成 json,所以您会得到一个包含多行内容的无效 json...
你可以轻松测试它here。
创建 json 字符串的正确方法是使用 json 序列化程序。由于您已经使用了 Json.Net,您可以像
var json = JsonConvert.SerializeObject(
new
{
description = textBox1.Text,
content = richTextBox1.Text,
@public = true
}
);