Webrequest未返回预期结果

时间:2017-03-02 08:03:01

标签: c# cookies httpwebrequest http-status-code-500

我试图从我们的时间管理服务中检索数据,以创建一个桌面客户端,当您接近最长工作时间时会向您发出警告。为此,我想使用HttpWebRequests。

这就是我现在所获得的(qnd):

class Program {
        private static readonly string mainurl = "[...]/autontlogin.jsp";
        private static readonly string mojouUri = "[...]/mojoudata.jsp";
static void Main(string[] args) {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(mainurl);
            CookieContainer jar = new CookieContainer();
            request.UseDefaultCredentials = true;
            request.Credentials = CredentialCache.DefaultNetworkCredentials;
            request.Method = "GET";
            request.ContentType = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
            request.CookieContainer = jar;
            request.UserAgent =
                "Mozilla/5.0 (Windows NT 10.0; WOW64; rv:45.0) Gecko/20100101 Firefox/45.0; Firefox 45.7.0 - 11712-1500630279-1.26";
            HttpWebResponse response = null;
            for (int i = 0; i < 5; i++) {
                try {
                    response = (HttpWebResponse)request.GetResponse();
                } catch (WebException e) {
                    Console.WriteLine(GetText(e.Response.GetResponseStream()));
                }
            }
            HttpWebRequest request2 = (HttpWebRequest)WebRequest.Create(mojouUri);
            request2.UseDefaultCredentials = true;
            request2.Credentials = CredentialCache.DefaultNetworkCredentials;
            request2.Method = "GET";
            request2.MediaType = "HTTP/1.1";
            request2.ContentType = "text/html";
            request2.CookieContainer = jar;
            request2.UserAgent =
                "Mozilla/5.0 (Windows NT 10.0; WOW64; rv:45.0) Gecko/20100101 Firefox/45.0; Firefox 45.7.0 - 11712-1500630279-1.26";
            var response2 = request2.GetResponse();
            Console.WriteLine(GetText(response2?.GetResponseStream()));

        }

        static string GetText(Stream s) {
            var sr = new StreamReader(s);
            return sr.ReadToEnd();
        }
    }

很遗憾,我无法与您分享确切的链接,但这不重要(这些服务器位于我们的内部网中)。

据我了解,我必须调用autontlogin.jsp来获取所需的Cookie(JSessionid) - 但遗憾的是我收到了内部服务器错误(500)。

当使用firefox插件(HttpRequester)(清除的cookie和缓存)调用相同的地址时,它按预期工作。我在构建请求时遗漏了什么?

修改1

导航到带有已清除缓存和Cookie的页面时会发生这种情况:

Overview

成功标题:

Successfull Header

第一个请求对象在标题中包含两个项目 - 内容类型和用户代理。

500错误响应sais:

<body>
<div id="header"><h1>Server Error</h1></div>
<div id="content">
 <div class="content-container"><fieldset>
  <h2>500 - Internal server error.</h2>
  <h3>There is a problem with the resource you are looking for, and it cannot be displayed.</h3>
 </fieldset></div>
</div>
</body>

希望这有帮助。

修改2

这是firefox插件HttpRequester所做的请求:

GET [...]/autontlogin.jsp

 -- response --
200 200
Content-Length:  1918
Content-Type:  text/html;charset=ISO-8859-1
Server:  Microsoft-IIS/7.5
Set-Cookie:  JSESSIONID=D2150640BEB566B6DACD7AD58B704A93;path=/WebClient/;Secure;HttpOnly userlocale=en-US%2Cen%3Bq%3D0.5;Max-Age=259200
Persistent-Auth:  true
Date:  Thu, 02 Mar 2017 08:05:56 GMT

0 个答案:

没有答案