TL; DR: I'm new to this language and have no idea what I'm doing
here is my class so 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
{
// get the 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 to do anything special here....
}
}
static void Main(string[] args)
{
MyClass.CreateObject();
}
}
when I do csc filename.cs, I get the following error:
The type or namespace name 'Http' does not exist in the namespace 'System.Net' (are you missing an assembly reference?)
HttpClient
lives in the System.Net.Http
namespace.
You'll need to add:
using System.Net.Http;
And make sure you are referencing System.Net.Http.dll
in .NET 4.5.
The code posted doesn't appear to do anything with webClient
. Is there something wrong with the code that is actually compiling using HttpWebRequest
?
Update
To open the Add Reference dialog right-click on your project in Solution Explorer and select Add Reference.... It should look something like: