日期:2014-05-18 浏览次数:21230 次
if (method.ToUpper() == "POST")
{
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = data.Length;
/* 可接收可压缩HTML*/
request.Headers.Add("Accept-Language", "zh-cn");
request.Headers.Add("Accept-Encoding", "gzip,deflate");
/* 柏志诚 */
Stream newStream = request.GetRequestStream();
newStream.Write(data, 0, data.Length);
newStream.Close();
}
------解决方案--------------------
request.Method = "GET";
HttpWebRequest req = (HttpWebRequest) HttpWebRequest.Create( "" );
req.Method = "GET";
using (WebResponse wr = req.GetResponse())
{
}
post
string param = "";
byte[] bs = Encoding.ASCII.GetBytes(param);
HttpWebRequest req = (HttpWebRequest) HttpWebRequest.Create( "" );
req.Method = "POST";
req.ContentType = "application/x-www-form-urlencoded";
req.ContentLength = bs.Length;
using (Stream reqStream = req.GetRequestStream())
{
reqStream.Write(bs, 0, bs.Length);
}
------解决方案--------------------
public String RGetPageCode(String PageURL, String Charset)
{
String strHtml = "";
ASCIIEncoding encoding = new ASCIIEncoding();
//byte[] bytes = Encoding.UTF8.GetBytes(Charset);
byte[] bytes = encoding.GetBytes(Charset);
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(PageURL);
request.Method = "POST";
request.ContentLength = bytes.Length;
request.ContentType = "text/xml";
using (Stream requestStream = request.GetRequestStream())
{
requestStream.Write(bytes, 0, bytes.Length);
}
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
if (response.StatusCode == HttpStatusCode.OK)
{
// strHtml = String.Format("POST failed. Received HTTP {0}",response.StatusCode);
if (response != null)
{
if (response.StatusCode == HttpStatusCode.OK && request.HaveResponse)
{
using (StreamReader sr = new StreamReader(response.GetResponseStream(), encoding))
{
if (sr != null)
{
strHtml = sr.ReadToEnd();
}
}
}
response.Close();
}
}
return strHtml;
}