繁体   English   中英

如何使用Rest API从C#Windows应用程序对ALM进行身份验证

[英]How To authenticate ALM from C# Windows Application Using Rest API

我有点卡在这里,试图使用C#Window application中的Rest API集成ALM。 但是第一步失败,那就是身份验证。

这是我的身份验证电话:

public void auth(string url, string xml)
    {
        HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
        byte[] requestBytes = System.Text.Encoding.UTF8.GetBytes(xml.ToString());
        req.Method = "POST";
        req.ContentType = "application/xml";
        req.Accept = "application/xml";
        req.KeepAlive = true;
        req.AllowAutoRedirect = true;
        req.ContentLength = requestBytes.Length;
        Stream requestStream = req.GetRequestStream();
        requestStream.Write(requestBytes, 0, requestBytes.Length);
        requestStream.Close();
        HttpWebResponse res = (HttpWebResponse)req.GetResponse();
        StreamReader sr = new StreamReader(res.GetResponseStream(), System.Text.Encoding.Default);
        backstr = sr.ReadToEnd();
        myheader = res.Headers.Get("Set-Cookie");
        sr.Close();
        res.Close();

    }

我这样称呼:

 try
        {
            // my creds, server details
            string server_ = @"https://MyAlmUrl";
            string  user_ = "username";
            string password_ = "password";
            string xmll = "<?xml version='1.0' encoding='utf-8'?><alm-authentication><user>" + user_ + "</user><password>" + password_ + "</password></alm-authentication>";
            rest.auth(server_ + "/authentication-point/authenticate", xmll);
        }

无论我做什么,它都会陷入: 401 unauthorized error

我确实具有有效的凭据,因为我可以使用相同的凭据通过Web登录。

有人帮我解决了,我做错了什么,因为此ALM和使用Rest API对我来说都是新的。

尝试这个 :

  HttpWebRequest myauthrequest = (HttpWebRequest)WebRequest.Create("http://{qcserver}/qcbin/authentication-point/alm-authenticate");

        string AuthenticationXML = @"<alm-authentication>
                        <user>{qcserver-username}</user>                
                        <password>{qcserver-pwd}</password>       
                        </alm-authentication>";

        byte[] Requestbytes = Encoding.UTF8.GetBytes(AuthenticationXML);
        myauthrequest.Method = "POST";
        myauthrequest.ContentType = "application/xml";
        myauthrequest.ContentLength = Requestbytes.Length;
        myauthrequest.Accept = "application/xml";
        Stream RequestStr = myauthrequest.GetRequestStream();
        RequestStr.Write(Requestbytes, 0, Requestbytes.Length);
        RequestStr.Close();
        HttpWebResponse myauthres = (HttpWebResponse)myauthrequest.GetResponse();
        var AuthenticationCookie = myauthres.Headers.Get("Set-Cookie");

       //request to get all domains in ALM
        HttpWebRequest domainreq = (HttpWebRequest)WebRequest.Create("http://{qcserver}/qcbin/rest/domains/");
        domainreq.Method = "GET";
        domainreq.ContentType = "application/xml";
        domainreq.Accept = "application/xml";
        domainreq.Headers.Set(HttpRequestHeader.Cookie, AuthenticationCookie);
        HttpWebResponse domainres= (HttpWebResponse)domainreq.GetResponse();
        Stream RStream = domainres.GetResponseStream();
        XDocument doc = XDocument.Load(RStream);

       //request to dowmload a particular attachement assigned to a test case
       HttpWebRequest attachmentreq = (HttpWebRequest)WebRequest.Create("http://{qcserver}/qcbin/rest/domains/{domainname}/project/{projectname}/tests/{testid}/attachments/file.xls");
        attachmentreq.Method = "GET";
        attachmentreq.ContentType = "application/xml";
        attachmentreq.Accept = "application/octet-stream";
        attachmentreq.Headers.Set(HttpRequestHeader.Cookie, AuthenticationCookie);
        HttpWebResponse attachmentres= (HttpWebResponse)attachmentreq.GetResponse();
        Stream RStream = attachmentres.GetResponseStream();
        var fileStream = File.Create("C:\\PathToFile");
        RStream.InputStream.CopyTo(fileStream);
        fileStream.Close();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM