To jest kod, który mam do tej pory:
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);
}
}
}
}
Problem polega na tym, że myślę, że blok wyjątków jest uruchamiany (ponieważ kiedy usuwam try-catch, dostaję komunikat o błędzie serwera (500). Ale ja nie widzę linii Console.Out, które umieściłem w bloku catch.
Moja konsola:
``none
Wątek 'vshost.NotifyLoad' (0x1a20) zakończył się z kodem 0 (0x0).
Wątek '
I'm using Visual Studio 2011 Beta, and .NET 4.5 Beta.
Moja sugestia byłaby taka, aby użyć RestSharp. Możesz wykonywać wywołania do usług REST i mieć je rzucone na obiekty POCO z bardzo małą ilością kodu kotłowego, aby faktycznie musieć parsować przez odpowiedź. Nie rozwiąże to twojego konkretnego błędu, ale odpowiada na twoje ogólne pytanie, jak wykonywać połączenia z usługami REST. Konieczność zmiany kodu, aby go użyć, powinna się opłacić w łatwości użycia i solidności idącej naprzód. To są tylko moje 2 centy
Niezwiązane, jestem pewien, ale zawijaj swoje obiekty IDisposable
w bloki using
, aby zapewnić właściwą utylizację:
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);
}
}
}
}
Ponieważ używasz Visual Studio 11 Beta, będziesz chciał użyć najnowszej i najwspanialszej wersji. Nowe Web Api zawiera klasy, które to umożliwiają.
Zobacz HttpClient: http://wcf.codeplex.com/wikipage?title=WCF%20HTTP