Şu ana kadar sahip olduğum kod bu:
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);
}
}
}
}
Sorun şu ki, istisna bloğunun tetiklendiğini düşünüyorum (çünkü try-catch'i kaldırdığımda bir sunucu hatası (500) mesajı alıyorum. Ancak catch bloğuna koyduğum Console.Out satırlarını göremiyorum.
Konsolum:
İş parçacığı 'vshost.NotifyLoad' (0x1a20) 0 (0x0) koduyla çıktı.
Thread '<No Name>' (0x1988) 0 (0x0) koduyla çıktı.
İş parçacığı 'vshost.LoadReference' (0x1710) 0 (0x0) koduyla çıktı.
'ConsoleApplication1.vshost.exe' (Yönetilen (v4.0.30319)): Yüklenen 'c:\users\l. preston sego iii\documents\visual studio 11\Projects\ConsoleApplication1\ConsoleApplication1\bin\Debug\ConsoleApplication1.exe', Semboller yüklendi.
'ConsoleApplication1.vshost.exe' (Yönetilen (v4.0.30319)): Yüklenen 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Configuration\v4.0_4.0.0.0__b03f5f7f11d50a3a\System.Configuration.dll', Sembollerin yüklenmesi atlandı. Modül optimize edildi ve 'Just My Code' hata ayıklayıcı seçeneği etkinleştirildi.
System.dll içinde 'System.Net.WebException' türünde bir ilk şans istisnası oluştu
İş parçacığı 'vshost.RunParkingWindow' (0x184c) 0 (0x0) koduyla çıktı.
Thread '<No Name>' (0x1810) 0 (0x0) koduyla çıktı.
Program '[2780] ConsoleApplication1.vshost.exe: Program Trace' programından 0 (0x0) koduyla çıkıldı.
Program '[2780] ConsoleApplication1.vshost.exe: Yönetilen (v4.0.30319)' programından 0 (0x0) koduyla çıkıldı.
Visual Studio 2011 Beta ve .NET 4.5 Beta kullanıyorum.
Benim önerim RestSharp kullanmak olacaktır. REST hizmetlerine çağrı yapabilir ve yanıtı gerçekten ayrıştırmak için çok az kazan kalıbı kodu ile POCO nesnelerine dönüştürmelerini sağlayabilirsiniz. Bu sizin özel hatanızı çözmeyecektir, ancak REST hizmetlerine nasıl çağrı yapılacağına ilişkin genel sorunuzu yanıtlar. Bunu kullanmak için kodunuzu değiştirmek zorunda kalmanız, ileride kullanım kolaylığı ve sağlamlık açısından karşılığını verecektir. Bu sadece benim 2 sentim
Alakası yok, eminim, ama IDisposable
nesnelerinizi using
bloklarına sararak düzgün bir şekilde atılmasını sağlayın:
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 kullandığınız için en son ve en iyisini kullanmak isteyeceksiniz. Yeni Web Api bunun için sınıflar içerir.
Bkz: HttpClient: http://wcf.codeplex.com/wikipage?title=WCF%20HTTP