我正在尝试发布代码我的要点帐户。如果我编写一行代码,它就可以工作。但如果我写多行代码,我就会收到错误。
例如,运行 [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
我无法在第二个示例中发布代码。我得到" 远程服务器返回错误:(400)错误请求。"错误。
也许你需要知道
textBox1.Text for gist description
内容正文的richTextBox1.Text - >不适用于多行后期数据
我的代码如下:
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();
}
我该如何解决这个问题?我该怎么办?
谢谢!
答案 0 :(得分:1)
内容正文的richTextBox1.Text - >不适用于多行后期数据
NewLine charachters应该在json字符串中转义...由于你使用字符串操作形成你的json,你会得到一个带有多行内容的无效json ......
您可以轻松地对其进行测试here。
创建json字符串的正确方法是使用json序列化程序。既然您已经使用了Json.Net,那么就可以像
那样使用它var json = JsonConvert.SerializeObject(
new
{
description = textBox1.Text,
content = richTextBox1.Text,
@public = true
}
);