I ran a console application yesterday to send and receive web request and web response and it works fine. Today I’m testing the same method on my form application and I get the error WebRequest Does Not Contain A Definition For Create. All my imports are the same. It’s a bit strange and I don’t know what I’m doing wrong. This is the code to my console application:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using HtmlAgilityPack;
using System.Net;
using System.IO;
namespace WebrequestCsharp
{
class Program
{
static void Main(string[] args)
{
StreamWriter sw;
sw = File.AppendText("c:\\Temp\\webresponse.txt");
//Create a Web-Request to an URL
HttpWebRequest HWR_Request = (HttpWebRequest)WebRequest.Create("http://www.hitta.se/ericsson/företag_och_personer");
//Send Web-Request and receive a Web-Response
HttpWebResponse HWR_Response = (HttpWebResponse)HWR_Request.GetResponse();
//Translate data from the Web-Response to a string
Stream S_DataStream = HWR_Response.GetResponseStream();
StreamReader SR_DataStream = new StreamReader(S_DataStream, Encoding.UTF8);
string s_ResponseString = SR_DataStream.ReadToEnd();
S_DataStream.Close();
sw.WriteLine(s_ResponseString);
sw.Flush();
sw.Close();
HtmlDocument doc = new HtmlDocument();
doc.Load("c:\\Temp\\webresponse.txt");
Console.ReadKey();
}
}
}
And this is my form application that cannot accept my Create:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using HtmlAgilityPack;
using System.Net;
using System.IO;
namespace PhoneFind
{
class WebRequest
{
private String url { get; set; }
private String searchEngine { get; set; }
HttpWebRequest HWR_Request = (HttpWebRequest)WebRequest.Create("http://www.hitta.se/ericsson/företag_och_personer");
public WebRequest(String url, String searchEngine)
{
this.url = url;
this.searchEngine = searchEngine;
}
// sends a request to the search engine
public String sendRequest(String url, String searchEngine)
{
switch (searchEngine){
case "hitta":
//Create a Web-Request to a URL
//Send Web-Request and receive a Web-Response
HttpWebResponse HWR_Response = (HttpWebResponse)HWR_Request.GetResponse();
//Translate data from the Web-Response to a string
Stream S_DataStream = HWR_Response.GetResponseStream();
StreamReader SR_DataStream = new StreamReader(S_DataStream, Encoding.UTF8);
string s_ResponseString = SR_DataStream.ReadToEnd();
S_DataStream.Close();
break;
}
return "";
}
}
}
In your form application, your class is named
WebRequest, so the compiler is looking for aCreatemethod in that class, rather thatSystem.Net.WebRequestclass. In order to resolve this, fully qualify theWebRequestin System.Net:Thanks to Amiram for clarifying that it wasn’t an ambiguous name conflict as I originally thought it might be.