• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

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

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

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        }

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
谈C/C++指针精髓(三)发布时间:2022-07-13
下一篇:
C#生成DLL,在Unity中导入/调用DLL发布时间:2022-07-13
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap