Search code examples
c#.netvisual-studio-2008

Subscribe through API .net C#


I have to submit subscription data to another web site. I have got documentation on how to use this API however i'm not 100% sure of how to set this up. I do have all the information needed, like username / passwords etc.

This is the API documentation:

https://www.apiemail.net/api/documentation/?SID=4

How would my request / post / whatever look like in C# .net (vs 2008) when i'm trying to acces this API?

This is what i have now, I think i'm not on the right track:

public static string GArequestResponseHelper(string url, string token, string username, string password)
{

HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(url);

myRequest.Headers.Add("Username: " + username);
myRequest.Headers.Add("Password: " + password);

HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();
         Stream responseBody = myResponse.GetResponseStream();

         Encoding encode = System.Text.Encoding.GetEncoding("utf-8");
         StreamReader readStream = new StreamReader(responseBody, encode);

         //return string itself (easier to work with)
         return readStream.ReadToEnd();

Hope someone knows how to set this up properly.


Solution

  • The documentation doesn't state clearly if the credentials should be passed as HTTP headers. They talk about parameters, so I would also try passing them as GET parameters:

    url = string.Format("{0}?Username={1}&Password={2}", url, username, password);
    HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(url);