static void Main(string[] args) { HttpWebResponseResult httpWebResponseResult = null; HttpWebClient httpWebClient = new HttpWebClient(); httpWebClient.Headers.Add("Referer", "http://localhost"); httpWebClient.IsTraceEnabled = true; //Example No.1 multipart/form-data POST reqeust String url = "http://localhost:53090/upload/uploadfile"; byte[] byteFiles = null; using (FileStream fs = new FileStream(System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "01.jpg"), FileMode.Open, FileAccess.Read)) { byteFiles = new byte[fs.Length]; fs.Read(byteFiles, 0, byteFiles.Length); } httpWebClient.SetField("StorageFile.Title", "title01", true); httpWebClient.SetField("StorageFile.Summary", "summary01", true); httpWebClient.SetField("filedata", byteFiles, "001.jpg", String.Empty); httpWebResponseResult = httpWebClient.Post(url); TraceInfo(httpWebResponseResult); //Example No.2 application/x-www-form-urlencoded POST reqeust url = "http://localhost:53090/passport/logon"; httpWebClient.SetField("LogonInfo.ValidateCode", "gvzwb"); httpWebClient.SetField("LogonInfo.User.Username", "sanxia123"); httpWebClient.SetField("LogonInfo.User.Password", "Yc123703"); httpWebClient.SetField("LogonInfo.IsPersistentCookie", "false"); httpWebResponseResult = httpWebClient.Post(url); TraceInfo(httpWebResponseResult); //Example No.3 GET reqeust url = "http://localhost:53090/content/oauth.html"; httpWebClient.SetField("a", "1"); httpWebResponseResult = httpWebClient.Get(url); TraceInfo(httpWebResponseResult); System.Console.ReadKey(true); }
/// <summary> /// 发送Http请求 /// </summary> /// <param name="url">请求Url</param> /// <param name="method">请求类型</param> /// <param name="parameters">参数集合</param> /// <returns></returns> private String Request(string url, RequestMethod method = RequestMethod.Get, params RequestOption[] parameters) { HttpWebClient httpWebClient = new HttpWebClient(); HttpWebResponseResult responseResult = null; #region 发起Http数据请求 switch (method) { case RequestMethod.Get: { #region //请求头和请求参数预处理 RequestHeaderExecuting(httpWebClient); parameters = RequestGetExecuting(parameters); #endregion #region //传递参数 foreach (var item in parameters) { httpWebClient.SetField(item.Name, (string)item.Value); } #endregion //发起请求 responseResult = httpWebClient.Get(url); } break; case RequestMethod.Post: { #region //请求头和请求参数预处理 RequestHeaderExecuting(httpWebClient); parameters = RequestPostExecuting(parameters); #endregion #region //判断当前POST是否为Multipart(即同时包含普通表单字段和文件表单字段) bool isMultipart = false; foreach (var item in parameters) { if (item.IsBinary) { isMultipart = true; break; } } #endregion #region //传递参数 if (isMultipart) { foreach (var item in parameters) { if (item.IsBinary) { httpWebClient.SetField(item.Name, (byte[])item.Value, String.Empty, String.Empty); } else { httpWebClient.SetField(item.Name, (string)item.Value, true); } } } else { foreach (var item in parameters) { httpWebClient.SetField(item.Name, (string)item.Value); } } #endregion //发起请求 responseResult = httpWebClient.Post(url); } break; } #endregion return responseResult.IsSuccess ? responseResult.ResponseText : String.Empty; }