1

How can I re-write the console app below using classes from .NET 3.5? What classes should I be looking into? The console application needs to be compiled with .net 3.5 and cannot use the HttpClient class. And it needs to be calling an WEB API and pass an XML string as I am doing below:

string xml = "<test>blah</test>";
HttpClient client = new HttpClient();
client.BaseAddress = new Uri("http://CONTOSO/");
HttpContent xmlContent = new StringContent(xml);

HttpResponseMessage response = client.PostAsync("API/Import/", xmlContent).Result;
if (response.IsSuccessStatusCode)
{
    Console.WriteLine(String.Format("Success."));
}
else
{
    Console.WriteLine("{0: } {1}", (int)response.StatusCode, response.ReasonPhrase);
}

I tried the code below but I get error: "ERROR: (500) Internal Server Error.":

string baseAddress = "http://CONTOSO/";
HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(baseAddress + "API/Import");
req.Method = "POST";
req.ContentType = "text/xml";
Stream reqStream = req.GetRequestStream();
string fileContents = xml;
byte[] fileToSend = Encoding.UTF8.GetBytes(fileContents);
reqStream.Write(fileToSend, 0, fileToSend.Length);
reqStream.Close();
HttpWebResponse resp = (HttpWebResponse)req.GetResponse(); //<== "ERROR: (500) Internal Server Error."
Console.WriteLine("HTTP/{0} {1} {2}", resp.ProtocolVersion, (int)resp.StatusCode, resp.StatusDescription);
2
  • you can use WebClient. Commented Jan 16, 2014 at 0:00
  • you don't want this line reqStream.Close(); Commented Jan 16, 2014 at 0:02

1 Answer 1

1

RestSharp does support .NET 3.5,

https://github.com/restsharp/RestSharp

Thus, you might use this open source HTTP client library to consume Web API.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.