C#呼叫winhttp元件 POST登入迅雷
阿新 • • 發佈:2019-01-03
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Thunder { /// <summary> /// COM物件的後期繫結呼叫類庫 /// </summary> public class ComObject { private System.Type _ObjType; private object ComInstance; /*public DxComObject() { throw new }*/ public ComObject(string ComName) { //根據COM物件的名稱建立COM物件 _ObjType = System.Type.GetTypeFromProgID(ComName); if (_ObjType == null) throw new Exception("指定的COM物件名稱無效"); ComInstance = System.Activator.CreateInstance(_ObjType); }public System.Type ComType { get { return _ObjType; } } //執行的函式 public object DoMethod(string MethodName, object[] args) { return ComType.InvokeMember(MethodName, System.Reflection.BindingFlags.InvokeMethod, null, ComInstance, args); }public object DoMethod(string MethodName, object[] args, System.Reflection.ParameterModifier[] ParamMods) { return ComType.InvokeMember(MethodName, System.Reflection.BindingFlags.InvokeMethod, null, ComInstance, args, ParamMods, null, null); } //獲得屬性與設定屬性 public object this[string propName] { get { return _ObjType.InvokeMember(propName, System.Reflection.BindingFlags.GetProperty, null, ComInstance, null); } set { _ObjType.InvokeMember(propName, System.Reflection.BindingFlags.SetProperty, null, ComInstance, new object[] { value }); } } } /// <summary> /// WinHttp物件庫 /// </summary> public class WinHttp { private ComObject HttpObj; private string _ContentType; private int _ContentLength; private bool _Active; private System.Collections.ArrayList PostDataList;//提交的資料欄位 public WinHttp() { //構建WinHttp物件 HttpObj = new ComObject("WinHttp.WinHttpRequest.5.1"); _ContentType = "application/x-www-form-urlencoded"; _ContentLength = 0; PostDataList = new System.Collections.ArrayList(); } //提交引數資訊的個數 public int PostDataCount { get{return PostDataList.Count;} } //設定Content-Type屬性 public string ContentType { get{return _ContentType;} set { if(!_Active) _ContentType = value; else if(_ContentType.CompareTo(value)!=0) { _ContentType = value; SetRequestHeader("Content-Type",_ContentType); } } } //物件是否是開啟狀態 public bool Active { get{return _Active;} } //設定Send資料的長度 public int ContentLength { get{return _ContentLength;} set { if(!_Active) _ContentLength = value; else if(_ContentLength != value) { _ContentLength = value; HttpObj.DoMethod("SetRequestHeader",new object[2]{"Content-Length",value}); } } } //執行之後返回的結果 public string ResponseBody { get { if(_Active) { ComObject AdoStream = new ComObject("Adodb.Stream"); AdoStream["Type"] = 1; AdoStream["Mode"] = 3; AdoStream.DoMethod("Open",new object[]{}); AdoStream.DoMethod("Write",new object[1]{HttpObj["ResponseBody"]}); AdoStream["Position"] = 0; AdoStream["Type"] = 2; AdoStream["Charset"] = "GB2312"; return AdoStream["ReadText"].ToString(); } else return ""; } } //設定請求頭 public string SetRequestHeader(string Header,object Value) { object obj; obj = HttpObj.DoMethod("SetRequestHeader",new object[]{Header,Value}); if(obj != null) return obj.ToString(); else return "True"; } //開啟URL執行OpenMethod方法,Async指定是否採用非同步方式呼叫,非同步方式不會阻塞 public string Open(string OpenMethod,string URL,bool Async) { object obj; obj = HttpObj.DoMethod("Open",new object[]{OpenMethod,URL,Async}); if (obj != null) { _Active = false; return obj.ToString(); } else { SetRequestHeader("Content-Type",_ContentType); SetRequestHeader("User-Agent","Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)"); if (_ContentLength != 0) SetRequestHeader("Content-Length",_ContentLength); _Active = true; return "True"; } } //傳送資料 public string Send(string body) { if(!_Active) return "False"; object obj; obj = HttpObj.DoMethod("Send",new object[1]{body}); if(obj != null) return obj.ToString(); else return "True"; } //清空提交資訊 public void ClearPostData() { this.PostDataList.Clear(); } //增加提交資料資訊 public void AddPostField(string FieldName,object Value) { this.PostDataList.Add(FieldName+"="+Value.ToString()); } //通過引數指定提交 public string Post() { if(!_Active) { return "False"; } string st=""; for(int i=0;i<this.PostDataList.Count;i++) { if(st != "") st = st + "&"+ PostDataList[i].ToString(); else st = PostDataList[i].ToString(); } this.ContentLength = st.Length; return Send(st); } //設定等待超時等 public string SetTimeouts(long ResolveTimeout,long ConnectTimeout,long SendTimeout,long ReceiveTimeout) { object obj; obj = HttpObj.DoMethod("SetTimeouts",new object[4]{ResolveTimeout,ConnectTimeout,SendTimeout,ReceiveTimeout}); if(obj != null) return obj.ToString(); else return "True"; } //等待資料提交完成 public string WaitForResponse(object Timeout,out bool Succeeded) { if(!_Active) {Succeeded = false;return "";} object obj; bool succ; succ = false; System.Reflection.ParameterModifier[] ParamesM; ParamesM = new System.Reflection.ParameterModifier[1]; ParamesM[0] = new System.Reflection.ParameterModifier (2); // 初始化為介面引數的個數 ParamesM[0][1] = true; // 設定第二個引數為返回引數 //ParamesM[1] = true; object[] ParamArray = new object[2]{Timeout,succ}; obj = HttpObj.DoMethod("WaitForResponse",ParamArray,ParamesM); System.Windows.Forms.MessageBox.Show(ParamArray[1].ToString()); Succeeded=bool.Parse(ParamArray[1].ToString()); //Succeeded = bool.Parse(ParamArray[1].ToString); if(obj != null) {return obj.ToString();} else return "True"; } public string GetResponseHeader(string Header,ref string Value) { if(!_Active) {Value="";return "";} object obj; /*string str; str = ""; System.Reflection.ParameterModifier[] Parames; Parames = new System.Reflection.ParameterModifier[1]; Parames[0] = new System.Reflection.ParameterModifier (2); // 初始化為介面引數的個數 Parames[0][1] = true; */// 設定第二個引數為返回引數 obj = HttpObj.DoMethod("GetResponseHeader",new object[2]{Header,Value}); //Value = str; if(obj != null) {return obj.ToString();} else return "True"; } public string GetAllResponseHeaders() { object obj; obj =HttpObj["GetAllResponseHeaders"]; if (obj != null) { return obj.ToString(); } else return "True"; } } }