How do I use HttpWebRequest GET method w/ ContentType="application/json"
        Posted  
        
            by 
                Stephen Patten
            
        on Stack Overflow
        
        See other posts from Stack Overflow
        
            or by Stephen Patten
        
        
        
        Published on 2011-01-10T05:59:50Z
        Indexed on 
            2011/01/11
            14:54 UTC
        
        
        Read the original article
        Hit count: 383
        
Hello,
This one is real simple, run this Silverlight4 example with the ContentType property commented out and you'll get back a response from from my service in xml. Now uncomment the property and run it and you'll get an ProtocolViolationException, what should happen is the service returns JSON formatted data.
HttpWebRequest wreq = 
(HttpWebRequest)WebRequestCreator.ClientHttp.Create(new Uri("http://stephenpattenconsulting.com/Services/GetFoodDescriptionsLookup(2100)"));
//wreq.ContentType = "application/json";
wreq.BeginGetResponse((cb) =>
{
    HttpWebRequest rq = cb.AsyncState as HttpWebRequest;
    HttpWebResponse resp = rq.EndGetResponse(cb) as HttpWebResponse; // Exception
    StreamReader rdr = new StreamReader(resp.GetResponseStream());
    string result =  rdr.ReadToEnd(); 
    rdr.Close();
}, wreq);
EDIT: While perusing SO, I noticed similar posts, like this one Why am I getting ProtocolViolationException when trying to use HttpWebRequest? and this one How do I use HttpWebRequest with GET method
EDIT: The WCF 4 end point is 'adapted' from this link http://geekswithblogs.net/michelotti/archive/2010/08/21/restful-wcf-services-with-no-svc-file-and-no-config.aspx.
I can use Fiddler's request builder to to construct the proper requests and changing the content type does yield the correct results.
© Stack Overflow or respective owner