我无法在github上创建一个问题,即使它应该工作。出于某种原因,我确实设法从中取出一些东西。
我要做的是上传一个json格式的字符串,该字符串具有与POST相同的功能。显然这是行不通的。
using System;
using System.Collections.Generic;
using System.Net;
using Newtonsoft.Json;
namespace TestProject
{
class Program
{
static void Main(string[] args)
{
CreateGithubIssue("Test Issue - C#", "This is just a test to check if i can manage to create an issue from C#.");
}
static string username = "someUser";
static string password = "somePassword";
static string repoIssueLink = "http://api.github.com/repos/someUser/someRepo/issues";
public static void CreateGithubIssue(string Title, string Description)
{
WebClient webClient = new WebClient();
webClient.Headers.Add("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)");
webClient.Credentials = new NetworkCredential(username, password);
string jsonPost = JsonConvert.SerializeObject(new Issue(Title, Description), Formatting.Indented);
string response = webClient.UploadString(repoIssueLink, jsonPost);
Console.WriteLine(response);
Console.WriteLine(jsonPost);
}
}
public class Issue
{
public string title;
public string body;
public List<string> assignees;
public int milestone;
public List<string> labels;
public Issue(string title = "Default Title", string body = "Default Body", List<string> assignees = null, int milestone = 0, List<string> labels = null)
{
if (assignees == null) assignees = new List<string>();
if (labels == null) labels = new List<string>();
this.title = title;
this.body = body;
this.assignees = assignees;
this.milestone = milestone;
this.labels = labels;
}
}
}
好吧,在问了其他人之后,我找到了答案。我将链接更改为HTTPS并使用此代码:
WebClient webClient = new WebClient();
webClient.Headers.Add("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)");
string credentials = Convert.ToBase64String(Encoding.ASCII.GetBytes(username + ":" + password));
webClient.Headers[HttpRequestHeader.Authorization] = string.Format("Basic {0}", credentials);
string jsonOutput = JsonConvert.SerializeObject(new Issue(Title, Description), Formatting.Indented);
string response = webClient.UploadString(repoIssueLink, jsonOutput);
Console.WriteLine(response);
Console.WriteLine(jsonOutput);