【发布时间】:2012-03-25 13:28:39
【问题描述】:
TL; DR:我是这种语言的新手,不知道自己在做什么
到目前为止,这是我的课:
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();
}
}
当我执行 csc filename.cs 时,我收到以下错误:
命名空间“System.Net”中不存在类型或命名空间名称“Http”(您是否缺少程序集引用?)
【问题讨论】:
-
您正在尝试从静态方法(
webClient字段)访问非静态字段。此外,你从来没有真正将它用于任何事情。你可以直接删除它。