您将如何使用此 Web 请求方法?:
try
{
var request = WebRequest.Create(uri);
using (var response = request.GetResponse())
{
using (var responseStream = response.GetResponseStream())
{
// Process the stream
}
}
}
catch (WebException ex)
{
if (ex.Status == WebExceptionStatus.ProtocolError &&
ex.Response != null)
{
var resp = (HttpWebResponse)ex.Response;
if (resp.StatusCode == HttpStatusCode.NotFound)
{
// Do something
}
else
{
// Do something else
}
}
else
{
// Do something else
}
}
}
我在一个网站上找到了上面的代码,但我不确定流的含义或你把 url 放在哪里:例如:http ://www.google.com我更喜欢使用此代码,但我无法获得else 语句来处理它,这就是我所需要的。
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(String.Format("URL", hashe));
try
{
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
}
catch (Exception ex)
{
if (ex.ToString().Contains("404"))
{
}
}