【问题标题】:How to get status code of a POST with Asp.Net HttpWebRequest如何使用 Asp.Net HttpWebRequest 获取 POST 的状态码
【发布时间】:2010-04-10 07:30:16
【问题描述】:

当我的网站的站点地图更新时,我正在尝试 ping Google,但我需要知道 Google 或任何其他服务返回哪个状态代码。我的代码如下:

HttpWebRequest rqst = (HttpWebRequest)WebRequest.Create("http://search.yahooapis.com/ping?sitemap=http%3a%2f%2fhasangursoy.com.tr%2fsitemap.xml");
rqst.Method = "POST";
rqst.ContentType = "text/xml";
rqst.ContentLength = 0;
rqst.Timeout = 3000;

rqst.GetResponse();

【问题讨论】:

    标签: asp.net httpwebrequest search-engine sitemap webrequest


    【解决方案1】:

    您需要使用响应 - 将其分配给 HttpWebResponse 变量:

    HttpWebResponse resp = (HttpWebResponse)rqst.GetResponse();
    HttpStatusCode respStatusCode = resp.StatusCode;
    

    HttpStatusCode 枚举会告诉你返回了什么状态码。

    【讨论】:

    • 代码没问题,但我得到“OK”作为状态码。我需要 200、404 等代码。
    • 我完成了。如果我使用 int respStatusCode = resp.StatusCode;这将返回状态代码。更多详情:msdn.microsoft.com/en-us/library/system.net.httpstatuscode.aspx
    • @Hasan Gürsoy - 它是一个枚举,所以是的,它将被隐式转换为整数。
    • @Hasan Gürsoy - 很高兴能帮上忙 :)
    【解决方案2】:

    试试 HttpWebResponse.StatusCode

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-11-22
      • 1970-01-01
      • 2011-02-26
      • 2011-08-13
      • 1970-01-01
      • 2014-05-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多