webClient请求JAVA超时解决方案

    private class MyWebClient: WebClient {
            protected override WebRequest GetWebRequest(Uri uri) {
                WebRequest w = base.GetWebRequest(uri);
                w.Timeout = 20 * 60 * 1000;
                return w;
            }
        }
        


        public static string  GetInnerModel(string str) {
            try {

        

                string url = "http://xx.com/AgileWSPX/services/rest/transferModel/";
                var rando = Random();
                var postString = str;
                var date = DateTime.Now.ToString("mmssffff");
                byte[] postData = Encoding.UTF8.GetBytes(postString);

                var code = Encrypt(date + rando,"dahuagdp");

                MyWebClient client = new MyWebClient();
                client.Headers.Add("Nonce","" + rando + "");
                client.Headers.Add("Authorization","" + code + "");
                client.Headers.Add("Content-Type","application/x-www-form-urlencoded;");

                var httpurl = url + date;
                byte[] responseData = client.UploadData(httpurl,"POST",postData);//得到返回字符流 
                var data = Encoding.UTF8.GetString(responseData);

                if(!string.IsNullOrEmpty(data)) {

                    var dataList = data.Split(',').Distinct();
                    data = string.Join(",",dataList.Select(p => p));
                }
                return data;
            } catch(Exception ex)
            {
                return "";
            }


        }
        private static string Random() {
            return DateTime.Now.ToString("mmssffff");
        }
        public static string Encrypt(string pToDecrypt,string sKey) {
            try {
                byte[] data = Encoding.UTF8.GetBytes(pToDecrypt);
                using(var des = new DESCryptoServiceProvider()) {
                    des.Key = Encoding.UTF8.GetBytes(sKey);
                    des.IV = Encoding.UTF8.GetBytes(sKey);
                    using(MemoryStream ms = new MemoryStream()) {
                        using(var cs = new CryptoStream(ms,des.CreateEncryptor(),CryptoStreamMode.Write)) {
                            cs.Write(data,0,data.Length);
                            cs.FlushFinalBlock();
                        }
                        return Convert.ToBase64String(ms.ToArray());
                    }
                }
            } catch {
                return "";
            }
        }

 

你可能感兴趣的:(webClient请求JAVA超时解决方案)