zoukankan      html  css  js  c++  java
  • C#向远程地址发送数据

            static string proxyIpAddress = AppConfig.GetProxyIpAddress;
            static string proxyUserName = AppConfig.GetProxyUserName;
            static string proxyPassword = AppConfig.GetProxyPassword;
    
            #region Get HttpClient Return String
            /// <summary>
            /// Get HttpClient Return String
            /// </summary>
            /// <param name="apiUrl">api Url</param>
            /// <returns></returns>
            static public string GetHttpClientReturnString(string apiUrl, string reqParams)
            {
                string result = string.Empty;
                try
                {
                    NetworkCredential proxyCredential = new NetworkCredential();
                    proxyCredential.UserName = proxyUserName;
                    proxyCredential.Password = proxyPassword;
    
                    WebProxy proxy = new WebProxy(proxyIpAddress);
                    proxy.Credentials = proxyCredential;
    
                    var httpClientHandler = new HttpClientHandler()
                    {
                        Proxy = proxy,
                    };
                    httpClientHandler.PreAuthenticate = true;
                    httpClientHandler.UseDefaultCredentials = false;
                    httpClientHandler.Credentials = proxyCredential;
                    var client = new HttpClient(handler: httpClientHandler, disposeHandler: true);
    
                    HttpContent content = new StringContent(reqParams);
                    content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json");
    
                    var responseString = client.GetStringAsync(apiUrl);
                    result = responseString.Result;
                }
                catch (Exception ex)
                {
                    result = ex.Message;
                }
                return result;
            }
            #endregion
    
            #region Get HttpWebResponse Return String
            /// <summary>
            /// Get HttpWebResponse Return String
            /// </summary>
            /// <param name="apiUrl">api Url</param>
            /// <param name="parameters">parameters</param>
            /// <param name="contentType">contentType default value is application/x-www-form-urlencoded</param>
            /// <param name="methord">request methord default value is POST</param>
            /// <param name="timeout">timeout default value is 300000</param>
            /// <returns>response string</returns>
            static public string GetHttpWebResponseReturnString(string apiUrl, Dictionary<string, object> parameters, string contentType = "application/x-www-form-urlencoded", string methord = "POST", int timeout = 300000)
            {
                string result = string.Empty;
                string responseText = string.Empty;
                try
                {
                    if (string.IsNullOrEmpty(apiUrl))
                    {
                        return "apiURl is null";
                    }
    
                    StringBuilder postData = new StringBuilder();
                    if (parameters != null && parameters.Count > 0)
                    {
                        foreach (var p in parameters)
                        {
                            if (postData.Length == 0)
                            {
                                postData.AppendFormat("{0}={1}", p.Key, p.Value);
                            }
                            else
                            {
                                postData.AppendFormat("&{0}={1}", p.Key, p.Value);
                            }
                        }
                    }
    
                    ServicePointManager.DefaultConnectionLimit = int.MaxValue;
    
                    NetworkCredential proxyCredential = new NetworkCredential();
                    proxyCredential.UserName = proxyUserName;
                    proxyCredential.Password = proxyPassword;
    
                    WebProxy proxy = new WebProxy(proxyIpAddress);
                    proxy.Credentials = proxyCredential;
    
                    HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(apiUrl);
                    myRequest.Credentials = proxyCredential;
                    myRequest.Proxy = proxy;
    
                    myRequest.Timeout = timeout;
                    myRequest.ServicePoint.MaxIdleTime = 1000;
                    if (!string.IsNullOrEmpty(contentType))
                    {
                        myRequest.ContentType = contentType;
                    }
                    myRequest.ServicePoint.Expect100Continue = false;
                    myRequest.Method = methord;
                    byte[] postByte = Encoding.UTF8.GetBytes(postData.ToString());
                    myRequest.ContentLength = postData.Length;
    
                    using (Stream writer = myRequest.GetRequestStream())
                    {
                        writer.Write(postByte, 0, postData.Length);
                    }
    
                    using (HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse())
                    {
                        using (StreamReader reader = new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8))
                        {
                            responseText = reader.ReadToEnd();
                        }
                    }
                    if (!string.IsNullOrEmpty(responseText))
                    {
                        result = responseText;
                    }
                    else
                    {
                        result = "The remote service is not responding. Please try again later.";
                    }
                }
                catch (Exception ex)
                {
                    result = string.Format("Request exception:{0}, please try again later.", ex.Message);
                }
                return result;
            }
    
            #endregion
    View Code
  • 相关阅读:
    linux基础命令笔记
    linux日常常用命令分析
    43.QQ聊天软件GUI窗口编写
    42.线程概念及线程池
    pycham中报:ModuleNotFoundError: No module named 'pymysql'
    python os模块
    python用类的方式创建线程---自创建类
    python server端并发聊天
    python文件上传
    python编码--解码
  • 原文地址:https://www.cnblogs.com/hofmann/p/11556471.html
Copyright © 2011-2022 走看看