0

I am trying to call a web api service from C#. Following is the working fiddler trace.

POST https://clientsite.com/getAPI.json HTTP/1.1

Content-Type: text/plain

user: user

password: pass

key: Key

Host: clientsite.com

Content-Length: 63

Expect: 100-continue

Connection: Close

{"objectID":"XXXXX","objectType":"YYYYY"}

============================================ Now I wrote the following code... HttpWebRequest req =HttpWebRequest)WebRequest.Create(@"https://clientsite.com/getAPI.json");

req.Method = "POST";

req.ContentType = "text/plain";

string strNewValue = "login=user&password=pass&key=Key&objectID=XXXXXX&objectType=YYYYY";

        byte[] byteArray = Encoding.UTF8.GetBytes(strNewValue);

        req.ContentLength = byteArray.Length;

        Stream dataStream = req.GetRequestStream();

        dataStream.Write(byteArray, 0, byteArray.Length);

        dataStream.Close();

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

I am getting an authentication error. Obviously it didn't like the way I composed the request. Could any one help make an exact request like the fiddler trace please? Help is greatly appreciated.

Gulumal
  • 59
  • 2
  • 8
  • 1
    Can you provide the API documentation - i.e. some documentation of what the API call should look like? – Gratus D. Feb 21 '18 at 21:38
  • You will pass the following parameters in the HTTP Header • username (login username) •password (login password) • key Thats all I can see in the given documentation – Gulumal Feb 21 '18 at 21:47
  • it doesn't look like your passing the username password in the header but in the body. You need something like: req.Headers.Add(xxxx); See this answer: https://stackoverflow.com/questions/4334521/httpwebrequest-using-basic-authentication – Gratus D. Feb 21 '18 at 22:03

0 Answers0