|
@@ -63,74 +63,83 @@ namespace SAGA.DotNetUtils.Http
|
|
|
{
|
|
|
System.GC.Collect();
|
|
|
var responseValue = string.Empty;
|
|
|
- var request = (HttpWebRequest)WebRequest.Create(EndPoint + parameters);
|
|
|
- try
|
|
|
+ bool retry = false;
|
|
|
+ do
|
|
|
{
|
|
|
- request.ServicePoint.Expect100Continue = false;
|
|
|
- request.ServicePoint.UseNagleAlgorithm = false;
|
|
|
- request.ServicePoint.ConnectionLimit = int.MaxValue;
|
|
|
- request.AllowWriteStreamBuffering = false;
|
|
|
- request.Proxy = null;
|
|
|
-
|
|
|
- request.Method = Method.ToString();
|
|
|
- request.ContentLength = 0;
|
|
|
- request.ContentType = ContentType;
|
|
|
- Stopwatch st=new Stopwatch();
|
|
|
- st.Start();
|
|
|
- if (PostData != null && Method == HttpVerb.POST)
|
|
|
+ retry = false;
|
|
|
+ var request = (HttpWebRequest)WebRequest.Create(EndPoint + parameters);
|
|
|
+ try
|
|
|
{
|
|
|
- //解决上传汉字乱码问题
|
|
|
- var encoding = new UTF8Encoding();
|
|
|
- //var bytes = Encoding.GetEncoding("iso-8859-1").GetBytes(PostData);
|
|
|
- var bytes = encoding.GetBytes(PostData);
|
|
|
- //var bytes = PostData;
|
|
|
- request.ContentLength = bytes.Length;
|
|
|
-
|
|
|
- using (var writeStream = request.GetRequestStream())
|
|
|
+ request.ServicePoint.Expect100Continue = false;
|
|
|
+ request.ServicePoint.UseNagleAlgorithm = false;
|
|
|
+ request.ServicePoint.ConnectionLimit = int.MaxValue;
|
|
|
+ request.AllowWriteStreamBuffering = false;
|
|
|
+ request.Proxy = null;
|
|
|
+
|
|
|
+ request.Method = Method.ToString();
|
|
|
+ request.ContentLength = 0;
|
|
|
+ request.ContentType = ContentType;
|
|
|
+ Stopwatch st = new Stopwatch();
|
|
|
+ st.Start();
|
|
|
+ if (PostData != null && Method == HttpVerb.POST)
|
|
|
{
|
|
|
-
|
|
|
- writeStream.Write(bytes, 0, bytes.Length);
|
|
|
+ //解决上传汉字乱码问题
|
|
|
+ var encoding = new UTF8Encoding();
|
|
|
+ //var bytes = Encoding.GetEncoding("iso-8859-1").GetBytes(PostData);
|
|
|
+ var bytes = encoding.GetBytes(PostData);
|
|
|
+ //var bytes = PostData;
|
|
|
+ request.ContentLength = bytes.Length;
|
|
|
+
|
|
|
+ using (var writeStream = request.GetRequestStream())
|
|
|
+ {
|
|
|
+
|
|
|
+ writeStream.Write(bytes, 0, bytes.Length);
|
|
|
+ }
|
|
|
}
|
|
|
- }
|
|
|
- using (var response = (HttpWebResponse)request.GetResponse())
|
|
|
- {
|
|
|
-
|
|
|
- if (response.StatusCode != HttpStatusCode.OK)
|
|
|
+ using (var response = (HttpWebResponse)request.GetResponse())
|
|
|
{
|
|
|
- var message = String.Format("Request failed. Received HTTP {0}", response.StatusCode);
|
|
|
- throw new ApplicationException(message);
|
|
|
- }
|
|
|
|
|
|
- // grab the response
|
|
|
- using (var responseStream = response.GetResponseStream())
|
|
|
- {
|
|
|
- if (responseStream != null)
|
|
|
- using (var reader = new StreamReader(responseStream))
|
|
|
- {
|
|
|
- responseValue = reader.ReadToEnd();
|
|
|
+ if (response.StatusCode != HttpStatusCode.OK)
|
|
|
+ {
|
|
|
+ var message = String.Format("Request failed. Received HTTP {0}", response.StatusCode);
|
|
|
+ throw new ApplicationException(message);
|
|
|
+ }
|
|
|
|
|
|
- }
|
|
|
- }
|
|
|
+ // grab the response
|
|
|
+ using (var responseStream = response.GetResponseStream())
|
|
|
+ {
|
|
|
+ if (responseStream != null)
|
|
|
+ using (var reader = new StreamReader(responseStream))
|
|
|
+ {
|
|
|
+ responseValue = reader.ReadToEnd();
|
|
|
+
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
+ }
|
|
|
+ Debug.WriteLine("Response:" + st.Elapsed + ":" + EndPoint + ":" + PostData);
|
|
|
}
|
|
|
- Debug.WriteLine("Response:"+st.Elapsed+":"+ EndPoint + ":" + PostData);
|
|
|
- }
|
|
|
- catch (Exception e)
|
|
|
- {
|
|
|
- List<string> list = new List<string>();
|
|
|
- list.Add(EndPoint);
|
|
|
- list.Add("\r\n");
|
|
|
- list.Add(Method.ToString());
|
|
|
- list.Add("\r\n");
|
|
|
- list.Add(PostData ?? "");
|
|
|
- list.Add("\r\n");
|
|
|
- list.Add(e.Message);
|
|
|
- MessageShowBase.Infomation(list);
|
|
|
- }
|
|
|
- finally
|
|
|
- {
|
|
|
- request.Abort();
|
|
|
- }
|
|
|
+ catch (Exception e)
|
|
|
+ {
|
|
|
+ List<string> list = new List<string>();
|
|
|
+ list.Add(EndPoint);
|
|
|
+ //list.Add("\r\n");
|
|
|
+ list.Add(Method.ToString());
|
|
|
+ //list.Add("\r\n");
|
|
|
+ list.Add(PostData ?? "");
|
|
|
+ //list.Add("\r\n");
|
|
|
+ list.Add(e.Message);
|
|
|
+ //命令异常,重试
|
|
|
+ retry = (MessageShowBase.Retry(string.Join("\r\n", list)));
|
|
|
+ if (!retry)
|
|
|
+ throw ;
|
|
|
+ }
|
|
|
+ finally
|
|
|
+ {
|
|
|
+ request.Abort();
|
|
|
+ }
|
|
|
+ } while (retry);
|
|
|
+
|
|
|
|
|
|
return responseValue;
|
|
|
}
|