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        }
  • 相关阅读:
    android中的一个圆角图片
    android中一个评分的控件
    C++ primer(第五版)中需要复习第二遍的知识点记录
    在ubuntu18.04上交叉编译opencv2.4.9
    C6748和音频ADC连接时候的TDM以及I2S格式问题
    C6748的启动方式问题
    AK5703的ALC
    cool edit工具介绍及使用
    CCS编译环境及TI仿真器的使用
    把YUV转化成opencv中的Mat格式的两行代码备份
  • 原文地址:https://www.cnblogs.com/pzstudyhard/p/4805885.html
Copyright © 2011-2022 走看看