zoukankan      html  css  js  c++  java
  • [转]Web后台模拟前端post(带NTLM验证)

    本文转自:http://www.cnblogs.com/pzstudyhard/p/4805885.html


    using System.Data;

    • using System.Net;
    • using System.IO;
    • using System.Net.Http;
    • using System.Web;
    • using System.Collections.Specialized;
    • using System.Web.Script.Serialization;
    • using System.Collections;
    • public string ToPackageJson(DataTable dt) //封装Json
    •         {
    •             Dictionary<string, string> dic1 = new Dictionary<string, string>();
    •             foreach (DataRow dr in dt.Rows)  
    •            {
    •                 foreach (DataColumn dc in dt.Columns)
    •                 {
    •                     dic1.Add(dc.ColumnName, dr[dc.ColumnName].ToString());  
    •                }
    •             }
    •             Dictionary<string, object> dic2 = new Dictionary<string, object>();
    •             dic2.Add(dt.TableName, dic1);
    •             JavaScriptSerializer javaScriptSerializer = new JavaScriptSerializer();
    •             javaScriptSerializer.MaxJsonLength = Int32.MaxValue; //取得最大数值
    •             return javaScriptSerializer.Serialize(dic2); //返回一个json字符串  {"dt.TableName":{"列名1":"列值1","列名2":"列值2","列名n":"列值n"}}
    •         }
    • public string ToPost(string postURL,string NTLM_UserName,string NTML_PassWord,DataTable dtToPost)       
    •     {
    •     //封装Json
    •            string strJson = ToPackageJson(dtToPost);
    •             //通过NTLM验证
    •     //1、创建空白的网站证书缓存
    •             System.Net.CredentialCache MyCredentialCache = new System.Net.CredentialCache();
    •     //指定以b2c用户通过NTLM身份验证
    •             MyCredentialCache.Add(new System.Uri(postURL), "NTLM", new System.Net.NetworkCredential(NTLM_UserName, NTML_PassWord));
    •             HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(postURL);
    •             httpWebRequest.Credentials = MyCredentialCache;  
    •            httpWebRequest.Method = "POST";  
    •            httpWebRequest.ContentType = "application/json;charset=UTF-8";
    •             //将Json字符串转化为字节
    •             byte[] postDataByte = Encoding.UTF8.GetBytes(strJson);
    •             httpWebRequest.ContentLength = postDataByte.Length;
    •             httpWebRequest.AllowAutoRedirect = false;
    •             httpWebRequest.KeepAlive = true;
    •             httpWebRequest.ContentLength = postDataByte.Length;
    •             //获取用于写入请求数据的Stream对象  
    •            Stream writer = httpWebRequest.GetRequestStream();
    •             //将请求参数写入流  
    •            writer.Write(postDataByte, 0, postDataByte.Length);
    •             //关闭请求流  
    •            writer.Close();  
    •            //http响应所返回的字符流   
    •           string responseResult = "";
    •             HttpWebResponse response = null;
    •            try  
    •            {  
    •                //获取http返回的响应流  
    •                response = (HttpWebResponse)httpWebRequest.GetResponse();  
    •            }
    •            catch (WebException ex)
    •            {                
    •       response = (HttpWebResponse)ex.Response;
    •            }  
    •            //读取响应流内容
    •             StreamReader sr = new StreamReader(response.GetResponseStream());
    •             responseResult = sr.ReadToEnd();
    •             //关闭读取器
    •             sr.Close();
    •             return responseResult;  
    •        }
  • 相关阅读:
    django quick start
    LiveWriter插入高亮代码插件介绍 基于SyntaxHighighter
    自动填充脚本使用及注意事项
    连接池错误
    Python3.3官方教程中文翻译1:开胃菜
    [译]科学计算可视化在andriod与ios实现的工具
    EXTGWT、GWT与EXTJS之间的关系
    Python3.3官方教程中文翻译2:使用Python解释器
    SAS9.1.3安装过程中反复出现重启动挂起的解决方案
    Sqlite 插入数据异常(乱码),看看是不是数据类型的错误
  • 原文地址:https://www.cnblogs.com/freeliver54/p/7125933.html
Copyright © 2011-2022 走看看