• c# 使用http摘要认证


    .net 使用http摘要认证,返回json数据

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Net;
    
    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                String outMsg="";
    
              String result=  Request("url",
                    "GET", "", "", out outMsg);
    
              Console.WriteLine(result);
    
              Console.WriteLine(outMsg);
    
              Console.ReadKey();
            }
    
            public static string Request(string sUrl, string sMethod, string sEntity, string sContentType,
        out string sMessage)
            {
                try
                {
                    sMessage = "";
                    using (System.Net.WebClient client = new System.Net.WebClient())
                    {
                        client.Credentials = CreateAuthenticateValue(sUrl);
    
                        WebHeaderCollection customerHeader= new WebHeaderCollection();
                        customerHeader.Add(HttpRequestHeader.Accept, "application/json");
                        customerHeader.Add(HttpRequestHeader.ContentType, "application/json;charset=UTF-8");
    
                        client.Headers = customerHeader;
    
                        Uri url = new Uri(sUrl);
                        byte[] bytes = Encoding.UTF8.GetBytes(sEntity);
                        byte[] buffer;
                        switch (sMethod.ToUpper())
                        {
                            case "GET":
                                buffer = client.DownloadData(url);
                                break;
                            case "POST":
                                buffer = client.UploadData(url, "POST", bytes);
                                break;
                            default:
                                buffer = client.UploadData(url, "POST", bytes);
                                break;
                        }
    
                        return Encoding.UTF8.GetString(buffer);
                    }
                }
                catch (WebException ex)
                {
                    sMessage = ex.Message;
                    var rsp = ex.Response as HttpWebResponse;
                    var httpStatusCode = rsp.StatusCode;
                    var authenticate = rsp.Headers.Get("WWW-Authenticate");
    
                    return "";
                }
                catch (Exception ex)
                {
                    sMessage = ex.Message;
                    return "";
                }
            }
    
            private static CredentialCache CreateAuthenticateValue(string sUrl)
            {
                CredentialCache credentialCache = new CredentialCache();
                credentialCache.Add(new Uri(sUrl), "Digest", new NetworkCredential("admin", "password"));
    
                return credentialCache;
            }  
        }
    }
  • 相关阅读:
    xslt转换xml常用知识(4)
    xslt转换xml
    Oracle10g数据库的4种存储形式 .转帖
    建立与Oracle数据库服务器连接的两种连接模式(专用服务器与共享服务器) .转帖
    HDU 4473 Exam 构造枚举
    zoj 1002 Fire Net (DFS搜索)
    POJ 2421 Constructing Roads 克鲁斯卡尔(Kruskal)算法
    POJ 1258 AgriNet 克鲁斯卡尔(Kruskal)算法&并查集
    POJ 2031 Building a Space Station
    判断一个数是否为整数(转)
  • 原文地址:https://www.cnblogs.com/wenming205/p/10387159.html
Copyright © 2020-2023  润新知