Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
333 views
in Technique[技术] by (71.8m points)

c# - Function as variable throwing error NotImplementedException unhandled

okay so I have added a function to my c# script to fetch ip address and send the output as string in a variable heres my source

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.Specialized;
using System.Net;
using System.IO;


namespace ConsoleApplication1
{
class Program
{
    static void Main(string[] args)
    {
        string URL = "http://localhost/test2.php";
        WebClient webClient = new WebClient();

        NameValueCollection formData = new NameValueCollection();
        formData["var1"] = formData["var1"] = string.Format("MachineName: {0}", System.Environment.MachineName);
        formData["var2"] = stringGetPublicIpAddress();
        formData["var3"] = "DGPASS";

        byte[] responseBytes = webClient.UploadValues(URL, "POST", formData);
        string responsefromserver = Encoding.UTF8.GetString(responseBytes);
        Console.WriteLine(responsefromserver);
        webClient.Dispose();
        System.Threading.Thread.Sleep(5000);
    }

    private static string stringGetPublicIpAddress()
    {
        throw new NotImplementedException();
    }
        private string GetPublicIpAddress()
    {
        var request = (HttpWebRequest)WebRequest.Create("http://ifconfig.me");

        request.UserAgent = "curl"; // this simulate curl linux command

        string publicIPAddress;

        request.Method = "GET";
        using (WebResponse response = request.GetResponse())
        {
            using (var reader = new StreamReader(response.GetResponseStream()))
            {
                publicIPAddress = reader.ReadToEnd();
            }
        }

        return publicIPAddress.Replace("
", "");

    }
    }
    }

basically I have created this function

private static string stringGetPublicIpAddress()

and I am sending it as a variable

formdata["var2"] = stringGetPublicIpAddress();

I am getting this error

throw new NotImplementedException();  === NotImplementedException was unhandled
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You... didn't implement the method. You have this:

private static string stringGetPublicIpAddress()
{
    throw new NotImplementedException();
}

So of course any time you call that method, it's going to throw that exception. It looks like you did implement the method you want here, though:

private string GetPublicIpAddress()
{
    // the rest of your code
}

Maybe this is the result of some copy/paste error? Try getting rid of the small method that throws the exception and changing the implemented method to be static:

private static string GetPublicIpAddress()
{
    // the rest of your code
}

Then update anywhere you call it from this:

stringGetPublicIpAddress();

to this:

GetPublicIpAddress();

This really just looks like copy/paste errors gone wrong in strange ways. Or perhaps you're struggling with the difference between static and instance methods? Maybe you implemented the method, but the compiler suggested that you needed a static method? There's a lot to read about static vs. instance methods/members which I won't really get into here. It's a significant concept in object-oriented programming.

In this particular case, since you're in the context of a static method in Main, anything you call from Main on that class (the Program class) also needs to be static, unless you create an instance of Program like this:

var program = new Program();

This would allow you to call instance methods on Program:

program.SomeNonStaticMethod();

But for a small application such as this, that isn't really necessary.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...