1 public string GetCostWebservide() 2 { 3 string uri = "http://125.77.254.142:8011/WXService.asmx/GetCost"; 4 HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(uri); 5 string param = String.Empty; 6 param += String.Format("callback=1&CarNo={0}&monthNum={1}&CarColor={2}", System.Web.HttpUtility.UrlEncode("闽D1F760"), "1", System.Web.HttpUtility.UrlEncode("蓝")); 7 //param = (param); 8 byte[] bs = Encoding.ASCII.GetBytes(param); 9 myHttpWebRequest.Method = "POST"; 10 myHttpWebRequest.ContentType = "application/x-www-form-urlencoded"; 11 myHttpWebRequest.ContentLength = bs.Length; 12 myHttpWebRequest.GetRequestStream().Write(bs, 0, bs.Length); 13 myHttpWebRequest.Credentials = CredentialCache.DefaultCredentials; 14 HttpWebResponse myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse(); 15 //如果无法访问外网或者 16 if (myHttpWebResponse.StatusCode != HttpStatusCode.OK) 17 { 18 myHttpWebResponse.Close(); 19 return uri; 20 } 21 else 22 { 23 Stream st = myHttpWebResponse.GetResponseStream(); 24 StreamReader sr = new StreamReader(st); 25 string res = sr.ReadToEnd(); 26 sr.Close(); 27 st.Close(); 28 myHttpWebResponse.Close(); 29 return res; 30 }
时间: 2024-10-22 12:30:08