Code
the using
the System;
the using the System.Collections.Generic;
the using the System.Linq;
the using the System.Text;
the using the System.IO;
the using the System.Net;
namespace Bookmark.HttpAPI
{
public class WebRequestAPI { Private static the CookieContainer Cookie = new new the CookieContainer (); # region synchronous data transmission a POST /// <Summary> /// transmits data by a POST /// </ Summary> /// <param name = "the Url"> URL </ param> ///
<param name="postDataStr"> Post数据 </param>
/// <param name="cookie"> Cookie容器 </param>
/// <returns></returns>
public static string SendDataByPost( string Url, string postDataStr)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
if (cookie.Count == 0 )
{
request.CookieContainer = new CookieContainer();
cookie = request.CookieContainer;
}
else
{
request.CookieContainer = cookie;
}
request.Method = " POST " ;
request.ContentType = " application/x-www-form-urlencoded " ;
request.ContentLength = postDataStr.Length;
Stream myRequestStream = request.GetRequestStream();
StreamWriter myStreamWriter = new StreamWriter(myRequestStream, Encoding.GetEncoding( " gb2312 " ));
myStreamWriter.Write(postDataStr);
myStreamWriter.Close();
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream myResponseStream = response.GetResponseStream();
StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding( " utf-8 " ));
string retString = myStreamReader.ReadToEnd();
myStreamReader.Close();
myResponseStream.Close();
return retString;
}
#endregion
#region Synchronous data transmission via the GET method /// <Summary> /// transmits data by GET method /// </ Summary> /// <param name = "the Url"> URL </ param> /// <param name = "postDataStr"> the GET data </ param> /// <param name = "Cookie"> the GET container </ param> /// <Returns> </ Returns> public static String SendDataByGET ( String the Url, String postDataStr) { the HttpWebRequest Request = (the HttpWebRequest) the WebRequest. Create(Url + (postDataStr ==""?
"" : " ? " ) + postDataStr);
if (cookie.Count == 0 )
{
request.CookieContainer = new CookieContainer();
cookie = request.CookieContainer;
}
else
{
request.CookieContainer = cookie;
}
request.Method = " GET " ;
request.ContentType = " text/html;charset=UTF-8 " ;
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream myResponseStream = response.GetResponseStream();
StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding( " utf-8 " ));
string retString = myStreamReader.ReadToEnd();
myStreamReader.Close();
myResponseStream.Close();
return retString;
}
#endregion
}
}
the using the System.Collections.Generic;
the using the System.Linq;
the using the System.Text;
the using the System.IO;
the using the System.Net;
namespace Bookmark.HttpAPI
{
public class WebRequestAPI { Private static the CookieContainer Cookie = new new the CookieContainer (); # region synchronous data transmission a POST /// <Summary> /// transmits data by a POST /// </ Summary> /// <param name = "the Url"> URL </ param> ///
<param name="postDataStr"> Post数据 </param>
/// <param name="cookie"> Cookie容器 </param>
/// <returns></returns>
public static string SendDataByPost( string Url, string postDataStr)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
if (cookie.Count == 0 )
{
request.CookieContainer = new CookieContainer();
cookie = request.CookieContainer;
}
else
{
request.CookieContainer = cookie;
}
request.Method = " POST " ;
request.ContentType = " application/x-www-form-urlencoded " ;
request.ContentLength = postDataStr.Length;
Stream myRequestStream = request.GetRequestStream();
StreamWriter myStreamWriter = new StreamWriter(myRequestStream, Encoding.GetEncoding( " gb2312 " ));
myStreamWriter.Write(postDataStr);
myStreamWriter.Close();
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream myResponseStream = response.GetResponseStream();
StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding( " utf-8 " ));
string retString = myStreamReader.ReadToEnd();
myStreamReader.Close();
myResponseStream.Close();
return retString;
}
#endregion
#region Synchronous data transmission via the GET method /// <Summary> /// transmits data by GET method /// </ Summary> /// <param name = "the Url"> URL </ param> /// <param name = "postDataStr"> the GET data </ param> /// <param name = "Cookie"> the GET container </ param> /// <Returns> </ Returns> public static String SendDataByGET ( String the Url, String postDataStr) { the HttpWebRequest Request = (the HttpWebRequest) the WebRequest. Create(Url + (postDataStr ==""?
"" : " ? " ) + postDataStr);
if (cookie.Count == 0 )
{
request.CookieContainer = new CookieContainer();
cookie = request.CookieContainer;
}
else
{
request.CookieContainer = cookie;
}
request.Method = " GET " ;
request.ContentType = " text/html;charset=UTF-8 " ;
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream myResponseStream = response.GetResponseStream();
StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding( " utf-8 " ));
string retString = myStreamReader.ReadToEnd();
myStreamReader.Close();
myResponseStream.Close();
return retString;
}
#endregion
}
}
Reproduced in: https: //www.cnblogs.com/hubcarl/archive/2010/05/11/1732492.html