/// <summary> /// 售房网下载数据 /// </summary> /// <param name="url"></param> /// <returns></returns> public static string GetResponseString(string url) { string _StrResponse = ""; HttpWebRequest _WebRequest = (HttpWebRequest)WebRequest.Create(url); _WebRequest.UserAgent = "MOZILLA/4.0 (COMPATIBLE; MSIE 7.0; WINDOWS NT 5.2; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)"; _WebRequest.AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip;//自动解压 _WebRequest.Method = "GET"; WebResponse _WebResponse = _WebRequest.GetResponse(); StreamReader _ResponseStream = new StreamReader(_WebResponse.GetResponseStream(), System.Text.Encoding.GetEncoding("GBK")); _StrResponse = _ResponseStream.ReadToEnd(); _WebResponse.Close(); _ResponseStream.Close(); return _StrResponse; } //发送post请求 public void ProcessRequest(HttpContext context) { HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create("http://fanyi.baidu.com/transcontent"); Encoding encoding = Encoding.UTF8; string param = "ie=utf-8&source=txt&query=hello&t=1327829764203&token=8a7dcbacb3ed72cad9f3fb079809a127&from=auto&to=auto"; //encoding.GetBytes(postData); byte[] bs = Encoding.ASCII.GetBytes(param); string responseData = String.Empty; req.Method = "POST"; req.ContentType = "application/x-www-form-urlencoded"; req.ContentLength = bs.Length; using (Stream reqStream = req.GetRequestStream())//发送post请求 { reqStream.Write(bs, 0, bs.Length); reqStream.Close(); } using (HttpWebResponse response = (HttpWebResponse)req.GetResponse()) { using (StreamReader reader = new StreamReader(response.GetResponseStream(),encoding)) { responseData = reader.ReadToEnd().ToString(); } context.Response.Write(responseData); } } //保存文件 WebRequest request = WebRequest.Create("url地址"); WebResponse response = request.GetResponse(); Stream reader = response.GetResponseStream(); FileStream writer = new FileStream(" 保存文件的绝对路径", FileMode.OpenOrCreate, FileAccess.Write); byte[] buff = new byte[512]; int c = 0; //实际读取的字节数 while ((c = reader.Read(buff, 0, buff.Length)) > 0) {
writer.Write(buff, 0, c); } writer.Close(); writer.Dispose(); reader.Close(); reader.Dispose(); response.Close();