0

I have a web api service that I've created as part of a separate project, and I can connect and consume the service without any problem by using HttpClient in .NET 4 and 4.5. However, HttpClient is not supported in .NET 3.5, and I need to be able to consume this service from an existing .NET 3.5 application. I've tried using WebClient, but it doesn't work. Here's my code:

WebClient client = new WebClient();
client.Headers.Add("content-type", "application/json");

string serialisedData = JsonConvert.SerializeObject(DATA);

var response = client.UploadString(URI, serialisedData);

var result = JsonConvert.DeserializeObject(response);

I've tried the solutions using WebClient offered here: Call web APIs in C# using .NET framework 3.5 and here: http://www.thepicketts.org/2012/11/how-to-access-webapi-from-a-net-3-5-client-in-c/

and this solution includes HttpClient code, event though it isn't even supported: http://forums.asp.net/t/1982540.aspx?Consume+Web+API+in+project

What am I doing wrong when trying to connect to web api using .NET 3.5?

Community
  • 1
  • 1
freakinthesun
  • 699
  • 1
  • 9
  • 19

1 Answers1

0

As I pointed out in the comments, the issue was that there was an action filter setup for the web api service which was causing the problem.

freakinthesun
  • 699
  • 1
  • 9
  • 19
  • Why was the action filter causing this issue -- was it badly coded/buggy or its mere existence caused your client to break? If the former, then you're fine. If the latter, then you might still need a better solution to access your web api because what would happen if you absolutely must have an action filter? Anyway, just wanted to mention the [RestSharp](http://restsharp.org/) client. It specifically states in the [ReadMe](https://github.com/restsharp/RestSharp) that it supports .NET 3.5 and later versions. – djikay Jul 02 '14 at 23:49