我正在编写一些C#2.0代码,它必须执行基本的HTTP GET和POST.我正在使用System.Net.HttpWebRequest发送两种类型的请求和System.Net.HttpWebResponse来接收这两种类型.我的GET代码如下:
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(String.Format("{0}?{1}",
URLToHit,
queryString));
request.Method = "GET";
request.Timeout = 1000; // set 1 sec. timeout
request.ProtocolVersion = HttpVersion.Version11; // use HTTP 1.1
try
{
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
}
catch(WebException e)
{
// If I do anything except swallow the exception here,
// I end up in some sort of endless loop in which the same WebException
// keeps being re-thrown by the GetResponse method. The exception is always
// right (ie: in cases when I'm …Run Code Online (Sandbox Code Playgroud)