首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >WebException重新抛出

WebException重新抛出
EN

Stack Overflow用户
提问于 2019-02-17 18:02:56
回答 4查看 86关注 0票数 0

下面是代码:

代码语言:javascript
复制
    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块将正常工作。

然而,当我尝试重新抛出异常时,程序崩溃了。

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2019-02-17 18:30:46

它应该会崩溃,因为你没有处理异常重新抛出,你需要在Main()方法中try catch,如下所示。

代码语言:javascript
复制
static void Main(string[] args)
{
    try
    {
        Post("https://stackoverflow.com/", "test");
    }
    catch (Exception)
    {
        //  Handle exception re-throw,
    }
}
票数 1
EN

Stack Overflow用户

发布于 2019-02-17 18:21:01

这样做:

代码语言:javascript
复制
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...)将状态设置为枚举。等,并添加一些更多的处理,因为我发布的示例不应该用于更大或生产关键的东西。

你提到这是一个控制台应用程序,如果它只做一个单一的请求,而不做任何其他事情,那么它应该可以很好地完成这项工作。

票数 0
EN

Stack Overflow用户

发布于 2019-02-17 18:35:38

使用此代码解决的问题:

代码语言:javascript
复制
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;
            }
        }
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54732078

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档