c# - System.Net.Http: missing from namespace? (using .net 4.5) -
tl; dr: i'm new language , have no idea i'm doing
here class far:
using system; using system.collections.generic; using system.net.http; using system.web; using system.net; using system.io; public class myclass { private const string url = "https://sub.domain.com/objects.json?api_key=123"; private const string data = @"{""object"":{""name"":""title""}}"; public static void createobject() { httpwebrequest request = (httpwebrequest)webrequest.create(url); request.method = "post"; request.contenttype = "application/json"; request.contentlength = data.length; streamwriter requestwriter = new streamwriter(request.getrequeststream(), system.text.encoding.ascii); requestwriter.write(data); requestwriter.close(); try { // response webresponse webresponse = request.getresponse(); stream webstream = webresponse.getresponsestream(); streamreader responsereader = new streamreader(webstream); string response = responsereader.readtoend(); responsereader.close(); } catch (webexception we) { string webexceptionmessage = we.message; } catch (exception ex) { // no need special here.... } } static void main(string[] args) { myclass.createobject(); } }
when csc filename.cs, following error:
the type or namespace name 'http' not exist in namespace 'system.net' (are missing assembly reference?)
httpclient
lives in system.net.http
namespace.
you'll need add:
using system.net.http;
and make sure referencing system.net.http.dll
in .net 4.5.
the code posted doesn't appear webclient
. there wrong code compiling using httpwebrequest
?
update
to open add reference dialog right-click on project in solution explorer , select add reference.... should like:
Comments
Post a Comment