Hello, I am trying to get a response from a URL using the following method

public void IsUrlReachable()
		{

			HttpWebRequest webRequest;
			WebResponse webResponse = null; //Need to assign this as null otherwise it does not compile
			try
			{
				webRequest = WebRequest.Create("");
				webRequest.Method = "GET";
				webRequest.KeepAlive = false;
				webResponse = webRequest.GetResponse(); //Assign to webResponse
			}
			catch (Exception)
			{
				Debug.WriteLine("Didn't get it");
				//Something goes wrong. Implement handling of this exception
			}
			finally
			{
				if (webResponse != null)
				{
					webResponse.Close(); //Close webresponse connection
				}
				webResponse = null; //To clear up the webresponse
				webRequest = null; //To clear up the webrequest
			}

		}

It tells me the method for WebRequest.Create is not defined so I just generated a sub automatically (Using Visual Studio 2008, if that helps).

internal static System.Net.HttpWebRequest Create(string p)
		{
			throw new NotImplementedException();
		}

It actually does communicate and is able to send data to my webservice yet it will always hit the NotImplementedException, I am guessing I need to put something within the Create method in the WebRequest class, but I have no idea what does here, I am finding it hard to find documentation on this, any guidance? Even if it's just a link

Thank you

Dani AI

Generated

Good catch, — the symptom you described (GetResponse runs but execution then hits a NotImplementedException stub named Create) is exactly what happens when Visual Studio auto-generates a local method that shadows the framework API. Quick ways to diagnose and fix it:

  • Right-click the symbol WebRequest and choose "Go To Definition" (or F12). If VS opens a file in your project instead of the System.Net type, you have a local/auto-generated Create method or a user class named WebRequest. Delete or rename that stub so calls resolve to the framework type.
  • Search the project for a generated method like Create(string) or for any class WebRequest you accidentally added. If you need to disambiguate immediately, call the framework explicitly: global::System.Net.WebRequest.Create(...).
  • Make sure using System.Net; is present and that no custom type name conflicts exist.

A few practical suggestions to avoid related problems and to improve robustness (especially since readers arrive here years later): prefer the modern HttpClient API when targeting .NET 4.5+ for clearer async code, built-in timeouts, and easier disposal:

// modern, recommended pattern (requires .NET 4.5+)
using var client = new HttpClient { Timeout = TimeSpan.FromSeconds(10) };
var resp = await client.GetAsync(new Uri("http://example.com/service.asmx"));
if (resp.IsSuccessStatusCode)
{
    var content = await resp.Content.ReadAsStringAsync();
    // handle content
}

If you must use HttpWebRequest (older frameworks), avoid manual nulling of variables, always dispose responses/streams (using blocks), set a Timeout, and catch WebException to inspect WebException.Response for server error details. 's suggestion to read the response stream is valid — combine that with proper disposal and status-code checks for a production-ready check.

Recommended Answers

All 2 Replies

Use this and specify the URL properly

private void RequestResponse()
    {
        try
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(@"");
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            StreamReader sr = new StreamReader(response.GetResponseStream());
            string strRead = sr.ReadToEnd();
            txtHTMLContent.Text = strRead;
            sr.Close();
        }
        catch
        {
      
        }
    }

Ahh I solved it

string url = "";

public bool IsUrlReachable(Uri url)
		{
			///Variable used 
			bool isUrlReachable = false;
			///Defines the request and response to the web server
			HttpWebRequest httpRequest = null;
			HttpWebResponse httpResponse = null;

			try
			{
				///Uses the hardcoded url to create a request
				httpRequest = (HttpWebRequest)WebRequest.Create(url);
				///"Gets" the response
				httpRequest.Method = "GET";
				httpResponse = (HttpWebResponse)httpRequest.GetResponse();
				///If it can get it then DmsWebService is accessible and alive
				isUrlReachable = true;
			}
			///Catches the predicted WebException if the DmsWebService is down
			catch (WebException)
			{
				isUrlReachable = false;
			}
			finally
			{
				///Closes the responses
				if (httpResponse != null)
					httpResponse.Close();
			}
			///Returns the boolean
			return isUrlReachable;
		}

I will mark it as solved

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.