下面是代码:
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块将正常工作。
然而,当我尝试重新抛出异常时,程序崩溃了。
发布于 2019-02-17 18:30:46
它应该会崩溃,因为你没有处理异常重新抛出,你需要在Main()方法中try catch,如下所示。
static void Main(string[] args)
{
try
{
Post("https://stackoverflow.com/", "test");
}
catch (Exception)
{
// Handle exception re-throw,
}
}发布于 2019-02-17 18:21:01
这样做:
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...)将状态设置为枚举。等,并添加一些更多的处理,因为我发布的示例不应该用于更大或生产关键的东西。
你提到这是一个控制台应用程序,如果它只做一个单一的请求,而不做任何其他事情,那么它应该可以很好地完成这项工作。
发布于 2019-02-17 18:35:38
使用此代码解决的问题:
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;
}
}
}
}https://stackoverflow.com/questions/54732078
复制相似问题