[转]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;  
  •        }

 

posted on 2017-07-06 13:53  freeliver54  阅读(296)  评论(0编辑  收藏  举报

导航