1 string result = "";
2 HttpWebRequest request = null;
3 HttpWebResponse response = null;
4 Stream reqStream = null;
5 StreamReader sr = null;
6 try
7 {
8 request = (HttpWebRequest)WebRequest.Create(url);
9 request.Method = "POST";
10 request.Timeout = timeout * 1000;
11 request.KeepAlive = false;
12 request.ContentType = "text/xml";
13 byte[] data = System.Text.Encoding.UTF8.GetBytes(xml);
14 request.ContentLength = data.Length;
15 reqStream = request.GetRequestStream();
16 reqStream.Write(data, 0, data.Length);
17 Thread.Sleep(200);//不加入Sleep容易超时
18 response = (HttpWebResponse)request.GetResponse();
19 sr = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
20 result = sr.ReadToEnd().Trim();
21 }
22 catch (System.Threading.ThreadAbortException e)
23 {
24 System.Threading.Thread.ResetAbort();
25 return result;
26 }
27 catch (WebException e)
28 {
29 return result;
30 }
31 catch (Exception e)
32 {
33 return result;
34 }
35 finally
36 {
37 if (reqStream != null)
38 {
39 reqStream.Close();
40 }
41 if (sr != null)
42 {
43 sr.Close();
44 }
45 if (response != null)
46 {
47 response.Close();
48 response = null;
49 }
50 if (request != null)
51 {
52 request.Abort();
53 request = null;
54 }
55 }
56 return result;