这是我到目前为止的代码。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System;
using System.Net.Http;
using System.Web;
using System.Net;
using System.IO;
namespace ConsoleProgram
{
public class Class1
{
private const string URL = "https://sub.domain.com/objects.json?api_key=123";
private const string DATA = @"{""object"":{""name"":""Name""}}";
static void Main(string[] args)
{
Class1.CreateObject();
}
private 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 {
WebResponse webResponse = request.GetResponse();
Stream webStream = webResponse.GetResponseStream();
StreamReader responseReader = new StreamReader(webStream);
string response = responseReader.ReadToEnd();
Console.Out.WriteLine(response);
responseReader.Close();
} catch (Exception e) {
Console.Out.WriteLine("-----------------");
Console.Out.WriteLine(e.Message);
}
}
}
}
问题是,我认为异常块被触发了(因为当我删除try-catch时,我得到一个服务器错误(500)信息。 但我没有看到我放在catch块中的Console.Out行。
我的控制台。
线程'vshost.NotifyLoad'(0x1a20)已经退出,代码为0(0x0)。
线程'<No Name>'(0x1988)已经退出,代码为0(0x0)。
线程'vshost.LoadReference'(0x1710)已经退出,代码为0(0x0)。
'ConsoleApplication1.vshost.exe'(托管(v4.0.30319))。Loaded 'c:\users\l. Preston Sego iii\documents\visual studio 11\Projects\ConsoleApplication1\ConsoleApplication1\bin\Debug\ConsoleApplication1.exe', Symbols loaded.
'ConsoleApplication1.vshost.exe'(托管(v4.0.30319))。Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Configuration\v4.0_4.0.0.0__b03f5f7f11d50a3a\System.Configuration.dll', 跳过加载符号。模块被优化,调试器选项'Just My Code'被启用。
在System.dll中发生了一个类型为'System.Net.WebException'的第一次机会异常。
线程'vshost.RunParkingWindow'(0x184c)已经退出,代码为0(0x0)。
线程'<No Name>'(0x1810)已经退出,代码为0(0x0)。
程序'[2780] ConsoleApplication1.vshost.exe。Program Trace'已经退出,代码为0(0x0)。
程序 '[2780] ConsoleApplication1.vshost.exe:Managed (v4.0.30319)'已退出,代码为0(0x0)。
```
我使用的是Visual Studio 2011 Beta,以及.NET 4.5 Beta。
与此无关,我确信,但请将你的 "IDisposable "对象包裹在 "using "块中,以确保妥善处理。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System;
using System.Web;
using System.Net;
using System.IO;
namespace ConsoleProgram
{
public class Class1
{
private const string URL = "https://sub.domain.com/objects.json?api_key=123";
private const string DATA = @"{""object"":{""name"":""Name""}}";
static void Main(string[] args)
{
Class1.CreateObject();
}
private static void CreateObject()
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(URL);
request.Method = "POST";
request.ContentType = "application/json";
request.ContentLength = DATA.Length;
using (Stream webStream = request.GetRequestStream())
using (StreamWriter requestWriter = new StreamWriter(webStream, System.Text.Encoding.ASCII))
{
requestWriter.Write(DATA);
}
try
{
WebResponse webResponse = request.GetResponse();
using (Stream webStream = webResponse.GetResponseStream() ?? Stream.Null)
using (StreamReader responseReader = new StreamReader(webStream))
{
string response = responseReader.ReadToEnd();
Console.Out.WriteLine(response);
}
}
catch (Exception e)
{
Console.Out.WriteLine("-----------------");
Console.Out.WriteLine(e.Message);
}
}
}
}
由于你使用的是Visual Studio 11 Beta,你会希望使用最新、最棒的版本。新的Web Api包含这方面的类。
参见HttpClient: http://wcf.codeplex.com/wikipage?title=WCF%20HTTP