/// <summary>
/// Gets a JSON object from the JIRA server using an HTTP GET
/// </summary>
/// <param name="server">The URL of the server e.g. http://myserver.com</param>
/// <param name="path">The REST path send the request to e.g. /rest/api/2/project</param>
/// <param name="username">The user ID required for authentication. null or empty will use anonymous access.</param>
/// <param name="password">The password required for authentication encoded using Base64. null or empty will use anonymous access.</param>
/// <returns>A JSON object containing the results</returns>
internal static JToken GetJson(string server, string path, string username, Base64String password, out string error)
{
JToken result;
error = null;
HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(server + path);
httpWebRequest.ContentType = "application/json";
httpWebRequest.ProtocolVersion = HttpVersion.Version11;
httpWebRequest.Method = "GET";
httpWebRequest.UserAgent = agent;
httpWebRequest.Proxy = WebRequest.GetSystemWebProxy();
// Add the required authentication header
if (!string.IsNullOrEmpty(username) && password.Length > 0)
{
Base64String authInfo = password.prefix(username + ":");
httpWebRequest.Headers["Authorization"] = "Basic " + authInfo;
}
try
{
HttpWebResponse httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
using (JsonTextReader streamReader = new JsonTextReader(new System.IO.StreamReader(httpResponse.GetResponseStream())))
{
result = JToken.ReadFrom(streamReader);
}
return(result);
}
catch (WebException e)
{
error = e.Message;
return(null);
}
}