简体   繁体   English

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

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

I am bit stuck here, trying to Integrate ALM using Rest API from C# Window application . 我有点卡在这里,试图使用C#Window application中的Rest API集成ALM。 But failed at the first step that is authentication . 但是第一步失败,那就是身份验证。

Here is my Authentication call : 这是我的身份验证电话:

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();

    }

which i am calling like this : 我这样称呼:

 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);
        }

No matter what ever i do It lands into : 401 unauthorized error . 无论我做什么,它都会陷入: 401 unauthorized error

I do have a valid credentials as i am able to login using the web with same credentials . 我确实具有有效的凭据,因为我可以使用相同的凭据通过Web登录。

Some one help me out, what i am doing wrong , As This ALM and using Rest API , both are new to me . 有人帮我解决了,我做错了什么,因为此ALM和使用Rest API对我来说都是新的。

Try this : 尝试这个 :

  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