cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

C#-based login fails with 401

Highlighted
Newbie

C#-based login fails with 401

This works if I do it in an HTML form on a browser, but fails with HTTP 401 when performed as below.

Any ideas why?


Note: <server>, <username>, and <password> are placeholders


static void Main(string[] args)

{

            HttpWebRequest httpWReq = (HttpWebRequest)WebRequest.Create("http://<server>/Thingworx/action-login");


            ASCIIEncoding encoding = new ASCIIEncoding();

            string postData = "thingworx-form-userid=<username>";

            postData += "&thingworx-form-password=<password>";

            postData += "&x-thingworx-session=true";

            byte[] data = encoding.GetBytes(postData);


            httpWReq.Method = "POST";

            httpWReq.ContentType = "application/x-www-form-urlencoded";

            httpWReq.ContentLength = data.Length;


            using (Stream stream = httpWReq.GetRequestStream())

            {

                stream.Write(data, 0, data.Length);

            }


            // This line throws the 401 exception

            HttpWebResponse response = (HttpWebResponse)httpWReq.GetResponse();


            string responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();

}



1 REPLY 1
Highlighted

Re: C#-based login fails with 401

I submitted this on Feb 2 2015 and it took until October 15th to get "approved"?

Why did that take 8 months?

Announcements

Thingworx Navigate content has a new home! Click here to access the new Thingworx Navigate forum! ______________________________