zoukankan      html  css  js  c++  java
  • C# 采用Basic Auth传递Post或者GET 数据

    摘自:http://www.cnblogs.com/starcrm/p/4837971.html

    public class JiraApi
    {
    private string m_Username;
    private string m_Password;

    public JiraApi(string username, string password)
    {
    m_Username = username;
    m_Password = password;
    }

    /// <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;

    }
    }
    return "error";

    }
    catch (WebException wex)
    {

    if (wex.Response != null)
    {

    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;

    }
    finally
    {
    if (errorResponse != null) errorResponse.Close();
    }
    }
    }
    else
    {
    //throw new Exception(wex.Message);
    return wex.Message;

    }
    }
    }

    /// <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);
    }
    }

     
  • 相关阅读:
    salt+jenkins+gitlab+ecs构建公司部署平台
    解决openstack实例主机名后缀问题
    测试CentOS-7-x86_64-Minimal-1708.iso这种光盘安装效果
    dbca建库--linux上使用vnc图形化安装oracle10g版本
    使用mediainfo工具统计每个视频文件(媒体文件)播放时长
    rsync命令简单用法介绍
    winscp工具和xshell连接linux机器时切换到root账户
    zabbix和iptables的nat表结合使用
    adb和机顶盒一些常识
    VirtualBox虚拟机禁止时间同步
  • 原文地址:https://www.cnblogs.com/a-mumu/p/5160410.html
Copyright © 2011-2022 走看看