zoukankan      html  css  js  c++  java
  • C#代码使用随记-C000:C#代码模拟前端发起POST请求(带NTLM验证)

    C#代码使用随记-C000:C#代码模拟前端发起POST请求(带NTLM验证)

     1 using System.Data;
     2 using System.Net;
     3 using System.IO;
     4 using System.Net.Http;
     5 using System.Web;
     6 using System.Collections.Specialized;
     7 using System.Web.Script.Serialization;
     8 using System.Collections;
     9 public string ToPackageJson(DataTable dt) //封装Json
    10         {
    11             Dictionary<string, string> dic1 = new Dictionary<string, string>();
    12             foreach (DataRow dr in dt.Rows)  
    13            {
    14                 foreach (DataColumn dc in dt.Columns)
    15                 {
    16                     dic1.Add(dc.ColumnName, dr[dc.ColumnName].ToString());  
    17                }
    18             }
    19             Dictionary<string, object> dic2 = new Dictionary<string, object>();
    20             dic2.Add(dt.TableName, dic1);
    21             JavaScriptSerializer javaScriptSerializer = new JavaScriptSerializer();
    22             javaScriptSerializer.MaxJsonLength = Int32.MaxValue; //取得最大数值
    23             return javaScriptSerializer.Serialize(dic2); //返回一个json字符串  {"dt.TableName":{"列名1":"列值1","列名2":"列值2","列名n":"列值n"}}
    24         }
    25 public string ToPost(string postURL,string NTLM_UserName,string NTML_PassWord,DataTable dtToPost)       
    26     {
    27     //封装Json
    28            string strJson = ToPackageJson(dtToPost);
    29             //通过NTLM验证
    30     //1、创建空白的网站证书缓存
    31             System.Net.CredentialCache MyCredentialCache = new System.Net.CredentialCache();
    32     //指定以b2c用户通过NTLM身份验证
    33             MyCredentialCache.Add(new System.Uri(postURL), "NTLM", new System.Net.NetworkCredential(NTLM_UserName, NTML_PassWord));
    34             HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(postURL);
    35             httpWebRequest.Credentials = MyCredentialCache;  
    36            httpWebRequest.Method = "POST";  
    37            httpWebRequest.ContentType = "application/json;charset=UTF-8";
    38               //httpWebRequest.Headers.Add(HttpRequestHeader.Authorization, authStr); //auth权限验证   
    39             //将Json字符串转化为字节
    40             byte[] postDataByte = Encoding.UTF8.GetBytes(strJson);
    41             httpWebRequest.ContentLength = postDataByte.Length;
    42             httpWebRequest.AllowAutoRedirect = false;
    43             httpWebRequest.KeepAlive = true;
    44             httpWebRequest.ContentLength = postDataByte.Length;
    45             //获取用于写入请求数据的Stream对象  
    46            Stream writer = httpWebRequest.GetRequestStream();
    47             //将请求参数写入流  
    48            writer.Write(postDataByte, 0, postDataByte.Length);
    49             //关闭请求流  
    50            writer.Close();  
    51            //http响应所返回的字符流   
    52           string responseResult = "";
    53             HttpWebResponse response = null;
    54            try  
    55            {  
    56                //获取http返回的响应流  
    57                response = (HttpWebResponse)httpWebRequest.GetResponse();  
    58            }
    59            catch (WebException ex)
    60            {                
    61       response = (HttpWebResponse)ex.Response;
    62            }  
    63            //读取响应流内容
    64             StreamReader sr = new StreamReader(response.GetResponseStream());
    65             responseResult = sr.ReadToEnd();
    66             //关闭读取器
    67             sr.Close();
    68             return responseResult;  
    69        }
  • 相关阅读:
    2020暑假项目-车辆派遣管理系统开发记录#8
    2020暑假项目-车辆派遣管理系统开发记录#7
    2020暑假项目-车辆派遣管理系统开发记录#6
    2020暑假项目-车辆派遣管理系统开发记录#5
    2020暑假项目-车辆派遣管理系统开发记录#4
    2020暑假项目-车辆派遣管理系统开发记录#3
    持续交付工具ThoughtWorks Go部署step by step
    mysql千万级大数据SQL查询优化
    分享MYSQL中的各种高可用技术(源自姜承尧大牛)
    我的MYSQL学习心得(十七) 复制
  • 原文地址:https://www.cnblogs.com/pzstudyhard/p/4805885.html
Copyright © 2011-2022 走看看