0

I have a class with some methods for web requests, for example:

public class BiPRORequests
{
    private readonly string HOST;

    public BiPRORequests()
    {
        HOST = "server";
    }
    public RemoteCertificateValidationCallback ServerCertificateValidationCallback { get; set; }

    public string authenticate(string un, string pw)
    {
        try
        {
            HttpWebRequest request = HttpWebRequest.CreateHttp(string.Format("https://{0}/bipro/authenticate", HOST));
            request.ServerCertificateValidationCallback += (sender, certificate, chain, sslPolicyErrors) => true;

            request.Method = "POST";
            request.ContentType = "text/xml";

            string postData = @"some connection string" + un + pw;

            byte[] byteArray = Encoding.UTF8.GetBytes(postData);
            request.ContentLength = byteArray.Length;
            Stream dataStream = request.GetRequestStream();
            dataStream.Write(byteArray, 0, byteArray.Length);
            dataStream.Close();

            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            string bipro;
            if (response.StatusCode == HttpStatusCode.OK)
            {
                dataStream = response.GetResponseStream();
                StreamReader reader = new StreamReader(dataStream);
                string responseFromServer = reader.ReadToEnd();
                string te = Convert.ToString(response.StatusCode);;
                bipro = responseFromServer.Substring(821, 36);

                reader.Close();
                dataStream.Close();
                response.Close();
                return bipro;
            }
            return "error: ";
        }

        catch (WebException ex)
        {
            using (var stream = ex.Response.GetResponseStream())
            using (var reader = new StreamReader(stream))
            {
                string te = reader.ReadToEnd();
            }
            string exe = Convert.ToString(ex.Message);
            return exe;
        } }

And I have a program in another file that looks like this at the moment:

 static void Main(string[] args)
    {
        BiPRORequests testcon = new BiPRORequests();
        string test = testcon.authenticate("username", "password"));  
    }

My question is, there are several variables that get declared in the authenticate method and I want to get the 'generated' values of those variables. I would need postData, responseFromServer and bipro, but the method can only return one value (bipro). Is there any other way to access the values in my main method?

(I am trying to get them so I can compare them to the correct values to see if there are any errors within the method. I know there are several similar questions on this site, but I haven't found one that could help me.)

Laura
  • 61
  • 1
  • 11
  • 4
    You should create a class containing all the variables you need and let your function return an instance of this class that you can later use and Modify or read from – Denis Schaf Mar 12 '19 at 09:23
  • 1
    create a class like this: `public class ServerData { public string postData = ""; public string responseFromServer = ""; public string bipro = ""; }` and a function returning the class type like this: `ServerData getServerData(string un, string pw) { ServerData sd = new ServerData(); //do fancy stuff sd.postData = getPostdata(); sd.responseFromServer = getServerResponse(); sd.postData = getWhateverbiproIs(); return sd; }` – Denis Schaf Mar 12 '19 at 09:33

1 Answers1

3

There are many way, depending on the quantity you need, the most succinct way for a few values is probably a Named Tuple

public (int SomeVal, string Another) authenticate(string un, string pw)
{
    ...
    return (2,"yay");
}

...

var result = testcon.authenticate("username", "password")); 
Console.WriteLine(result.SomeVal);
Console.WriteLine(result.Another);

C# tuple types

Otherwise you might want to create a class

public class Something
{

    // properties

    // easy constructor 
}


...

public Something authenticate(string un, string pw)
{

    ...
    return new Something(blah,blah);
}
TheGeneral
  • 79,002
  • 9
  • 103
  • 141
  • 3
    Please let OP get familiar with classes first before we throw them into tuples. – Patrick Hofman Mar 12 '19 at 09:24
  • @PatrickHofman yup getting there, thanks – TheGeneral Mar 12 '19 at 09:25
  • @PatrickHofman thanks for watching out for me but the tulpe actually worked well for me and I understood it's basic principle easily :) – Laura Mar 12 '19 at 09:47
  • So you know all caveats of tuple? Be very careful. @Laura – Patrick Hofman Mar 12 '19 at 09:48
  • @PatrickHofman I do not and I definitely wil be, I just meant the solution Michael presented was easy to follow for me, and thats all. Of course if I wanted to use them in a more complex way I'd have some intensive research to do, but that's not the case right now – Laura Mar 12 '19 at 09:52