摘自:http://www.cnblogs.com/starcrm/p/4837971.htmlhtml
public class JiraApi
{
private string m_Username;
private string m_Password;json
public JiraApi(string username, string password)
{
m_Username = username;
m_Password = password;
}api
/// <summary>
/// 處理post請求,執行新建、編輯、刪除等操做
/// </summary>
/// <param name="sData">json輸入字符</param>
/// <param name="uri">api的具體地址,通常是baseurl + 業務處理資源關鍵字</param>
/// <returns>Jira返回的WebResponse輸出</returns>
public string DoPost(string sData, string uri)
{
Uri address = new Uri(uri);
HttpWebRequest request;
//HttpWebResponse response1 = null;
StreamReader sr;
string returnXML = string.Empty;
if (address == null) { throw new ArgumentNullException("address"); }
try
{
request = WebRequest.Create(address) as HttpWebRequest;
request.Method = "POST";
request.ContentType = "application/json";
string base64Credentials = GetEncodedCredentials();
request.Headers.Add("Authorization", "Basic " + base64Credentials);
//request.Credentials = new NetworkCredential(sUsername, sPassword);
if (sData != null)
{
byte[] byteData = UTF8Encoding.UTF8.GetBytes(sData);
request.ContentLength = byteData.Length;
using (Stream postStream = request.GetRequestStream())
{
postStream.Write(byteData, 0, byteData.Length);
}
using (HttpWebResponse response1 = request.GetResponse() as HttpWebResponse)
{
StreamReader reader = new StreamReader(response1.GetResponseStream());
string str = reader.ReadToEnd();
return str;app
}
}
return "error";post
}
catch (WebException wex)
{url
if (wex.Response != null)
{spa
using (HttpWebResponse errorResponse = (HttpWebResponse)wex.Response)
{
try
{
string sError = string.Format("The server returned '{0}' with the status code {1} ({2:d}).",
errorResponse.StatusDescription, errorResponse.StatusCode,
errorResponse.StatusCode);
sr = new StreamReader(errorResponse.GetResponseStream(), Encoding.UTF8);
returnXML = sr.ReadToEnd();
return returnXML;code
}
finally
{
if (errorResponse != null) errorResponse.Close();
}
}
}
else
{
//throw new Exception(wex.Message);
return wex.Message;orm
}
}
}server
/// <summary>
/// 處理get請求,執行查詢操做
/// </summary>
/// <param name="resource">輸入的業務處理資源關鍵字,必填項</param>
/// <param name="argument">參數,用於獲取具體查詢操做,非必填項</param>
/// <param name="data">暫時沒用處,非必填項</param>
/// <param name="method">默認爲GET,非必填項</param>
/// <returns></returns>
public string DoQuery(
string resource,
string argument = null,
string data = null,
string method = "GET")
{
string url = string.Format("{0}{1}/", Config.BaseURL, resource.ToString());
if (argument != null)
{
url = string.Format("{0}{1}/", url, argument);
}
HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
request.ContentType = "application/json";
request.Method = method;
if (data != null)
{
using (StreamWriter writer = new StreamWriter(request.GetRequestStream()))
{
writer.Write(data);
}
}
string base64Credentials = GetEncodedCredentials();
request.Headers.Add("Authorization", "Basic " + base64Credentials);
HttpWebResponse response = request.GetResponse() as HttpWebResponse;
string result = string.Empty;
using (StreamReader reader = new StreamReader(response.GetResponseStream()))
{
result = reader.ReadToEnd();
}
return result;
}
private string GetEncodedCredentials(){string mergedCredentials = string.Format("{0}:{1}", m_Username, m_Password);byte[] byteCredentials = UTF8Encoding.UTF8.GetBytes(mergedCredentials);return Convert.ToBase64String(byteCredentials);}}