服務器拒絕請求碼?使用HTTP請求目标地址時出現問題描述,,我來為大家講解一下關于服務器拒絕請求碼?跟着小編一起來看一看吧!
使用HTTP請求目标地址時出現問題描述,
使用EXE程序,能正常請求目标地址并獲取值,代碼如下:
///<summary> ///Description:提交請求URL數據 ///</summary> ///<param name="URL">Url地址</param> ///<param name="ArgData">發送的數據</param> ///<param name="Method">Http請求模式(Post/Get)</param> ///<returns>返回窗體内容</returns> public static string RequestUrl(string requestUrl, string postData, HTTPTYPE reqType) { WebClient client = new WebClient(); string IsProxy = System.Configuration.ConfigurationManager.AppSettings["IsProxy"]; if (IsProxy == "true") { string ProxyHost = System.Configuration.ConfigurationManager.AppSettings["ProxyHost"]; if (!string.IsNullOrEmpty(ProxyHost)) { string ProxyPort = System.Configuration.ConfigurationManager.AppSettings["ProxyPort"]; WebProxy p = new WebProxy(); Uri u = new Uri("http://" ProxyHost ":" ProxyPort); p.Address = u;
|
/// <summary> /// WEB請求幫助類 /// </summary> /// <param name="url">請求地址</param> /// <param name="postData">post參數</param> /// <param name="reqType">請求方式</param> /// <returns></returns> public static string webRequset(string requestUrl, string postData,HTTPTYPE reqType) { string str = ""; StringBuilder sb = new StringBuilder(); try { sb.AppendLine("請求 ====> " requestUrl); sb.AppendLine("入參 ====> " postData); WebClient client = new WebClient(); string IsProxy = System.Configuration.ConfigurationManager.AppSettings["IsProxy"]; if (IsProxy == "true") { sb.AppendLine("已啟用代理"); string ProxyHost = System.Configuration.ConfigurationManager.AppSettings["ProxyHost"]; if (!string.IsNullOrEmpty(ProxyHost)) { string ProxyPort = System.Configuration.ConfigurationManager.AppSettings["ProxyPort"]; WebProxy p = new WebProxy(); Uri u = new Uri("http://" ProxyHost ":" ProxyPort); p.Address = u; p.BypassProxyOnLocal = true; client.Proxy = p; } } if (reqType == HTTPTYPE.GET) { sb.AppendLine("請求方式 ===> GET"); str = client.DownloadString(string.Format("{0}?{1}", requestUrl, postData)); } else { sb.AppendLine("請求方式 ===> POST"); byte[] bytes = Encoding.UTF8.GetBytes(postData); client.Headers.Add("Content-Type", "application/json"); client.Headers.Add("ContentLength", postData.Length.ToString()); client.Headers.Add("ENCRTYPT_TYPE", "0"); client.Headers.Add("DATA_TYPE", "0"); client.Headers.Add("CHAR_SET", "UTF-8"); client.Headers.Add("MAX_WATI", "6000"); byte[] ret = client.UploadData(requestUrl, "POST", bytes); str = HttpUtility.UrlDecode(ret, System.Text.Encoding.GetEncoding("UTF-8")); } sb.AppendLine("返回 ====> " str); return str; } catch (Exception ex) { sb.AppendLine("異常:" ex.Message.ToString()); return str; } finally { ErrorLog.WriteLog(sb.ToString()); } } |
更多精彩资讯请关注tft每日頭條,我们将持续为您更新最新资讯!