WebException重新抛出

时间:2019-02-17 10:02:56

标签: c# .net

这是以下代码:

    public static string Post(string requestUriString, string s)
    {
        var request = (HttpWebRequest)WebRequest.Create(requestUriString);
        var data = Encoding.ASCII.GetBytes(s);
        request.Method = "POST";
        request.ContentType = "application/x-www-form-urlencoded";
        request.ContentLength = data.Length;

        using (var stream = request.GetRequestStream())
        {
            stream.Write(data, 0, data.Length);
        }

        try
        {
            var response = (HttpWebResponse)request.GetResponse();
            return new StreamReader(response.GetResponseStream()).ReadToEnd();
        }
        catch (WebException webException)
        {
            Console.WriteLine(webException);
            throw;
        }
    }

当程序引发异常时,try / catch块正常工作。

但是,当我尝试重新抛出异常时,程序崩溃。

4 个答案:

答案 0 :(得分:1)

由于您不处理异常重新抛出,它应该崩溃,您需要使用try方法中的catch Main(),如下所示。

static void Main(string[] args)
{
    try
    {
        Post("https://stackoverflow.com/", "test");
    }
    catch (Exception)
    {
        //  Handle exception re-throw,
    }
}

答案 1 :(得分:0)

执行以下操作:

class Program {
    static void Main(string[] args) {
        var response = Post(...);

        if(response.Status == "success") {
            //ok
        } else {
            //request failed
        }
    }

    public static string Post(string requestUriString, string s) {
        var request = (HttpWebRequest)WebRequest.Create(requestUriString);
        var data = Encoding.ASCII.GetBytes(s);
        request.Method = "POST";
        request.ContentType = "application/x-www-form-urlencoded";
        request.ContentLength = data.Length;

        using (var stream = request.GetRequestStream()) {
            stream.Write(data, 0, data.Length);
        }

        try {
            var response = (HttpWebResponse)request.GetResponse();
            var body = new StreamReader(response.GetResponseStream()).ReadToEnd();
            return new Response("success", body);
        } catch (WebException webException) {
            Console.WriteLine(webException);
            return new Response("failed", "");
        }
    }

    class Response {
        public string Status { get; private set; }
        public string Body { get; private set; }

        public Response(string status, string response) {
            Status = status;
            Body = response;
        }
    }
}

根据您的需求和应用程序大小,您可以基于http响应代码(200/404/500 ...)等将状态设置为枚举,并添加更多处理方式,因为我发布的示例不是您应该使用的用于更大的产品或对生产至关重要的产品。

您提到它是一个控制台应用程序,如果它只是执行单个请求的东西,那么别的什么都做不了。

答案 2 :(得分:0)

此代码解决了问题:

using System.Net;
using System.Text;
using System.IO;
using System;

namespace Botnet
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                Console.WriteLine(Post("http://www.example.com/recepticle.aspx", "thing1=hello&thing2=world"));
            }
            catch (WebException webException)
            {
                Console.WriteLine(webException);
            }
            finally
            {
                Console.ReadLine();
            }
        }

        public static string Post(string requestUriString, string s)
        {
            var request = (HttpWebRequest)WebRequest.Create(requestUriString);
            var data = Encoding.ASCII.GetBytes(s);
            request.Method = "POST";
            request.ContentType = "application/x-www-form-urlencoded";
            request.ContentLength = data.Length;

            using (var stream = request.GetRequestStream())
            {
                stream.Write(data, 0, data.Length);
            }

            try
            {
                var response = (HttpWebResponse)request.GetResponse();
                return new StreamReader(response.GetResponseStream()).ReadToEnd();
            }
            catch (WebException webException)
            {
                Console.WriteLine(webException);
                throw;
            }
        }
    }
}

答案 3 :(得分:0)

嗨,我将对先前的答案进行一些修改。

class Response<T> {
        public HttpStatusCode Status { get; private set; }
        public T Body { get; private set; }
        public bool Success { get; private set; }
        public string Reason {get; private set; }

        public Response(T body) {
            Success = true;
            Body = body;
            HttpStatusCode = HttpStatusCode.OK
        }

        public Response(string reason)
        {
            Reason = reason;
            Success = false;
            Status = HttpStatusCode.BadRequest;
        }

        public Response( string reason, HttpStatusCode status)
        {
            Reason = reason;
            Status = status;
            Success = false;
        }
    }

这是更通用的解决方案。