How to use the HttpWebRequest GET method w / ContentType = "application / json"

It is very simple, run this Silverlight4 example while preserving the ContentType property, and you will get a response from my service in xml. Now uncomment the property and run it, and you will get a ProtocolViolationException, which should happen that the service returns data in JSON format.

#if DEBUG
                Address = "http://localhost.:55437/Services/GetFoodDescriptionsLookup(2100)";
#else
                Address = "http://stephenpattenconsulting.com/Services/GetFoodDescriptionsLookup(2100)";
#endif

Uri httpSite = new Uri(Address);

HttpWebRequest wreq = 
(HttpWebRequest)WebRequestCreator.ClientHttp.Create(httpSite);

//wreq.ContentType = "application/json"; // Wrong
wreq.Accept = "application/json"; // Right

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);

New exception

System.NotSupportedException Message = "" :       System.Net.Browser.AsyncHelper.BeginOnUI(SendOrPostCallback beginMethod, )       System.Net.Browser.BrowserHttpWebRequest.EndGetResponse(IAsyncResult asyncResult)       com.patten.silverlight.ViewModels.WebRequestLiteViewModel.b_0 (IAsyncResult cb)       System.Net.Browser.BrowserHttpWebRequest. < > c_DisplayClassd.b__b (Object state2)       System.Threading.QueueUserWorkItemCallback.WaitCallback_Context ( )       System.Threading.ExecutionContext.Run(ExecutionContext executeContext, ContextCallback, , Boolean ignoreSyncCtx)       System.Threading.QueueUserWorkItemCallback.System.Threading.IThreadPoolWorkItem.ExecuteWorkItem()       System.Threading.ThreadPoolWorkQueue.Dispatch()       System.Threading.ThreadPoolWaitCallback.PerformWaitCallback() InnerException: System.NotSupportedException      Message = .       :            System.Net.Browser.BrowserHttpWebRequest.InternalEndGetResponse(IAsyncResult asyncResult)            System.Net.Browser.BrowserHttpWebRequest. < > c_DisplayClass5.b_4 (Object sendState)            System.Net.Browser.AsyncHelper. < > c_DisplayClass2.b__0 (Object sendState)      InnerException:

, , , , Fiddler , .. http://localhost.:55437/Services/GetFoodDescriptionsLookup(2100), DOT localhost.

!

, , , .. , URI, , SO, . ().

, , , stackoverflow, .

jQuery, , " " "", SL4. ( !)

function CallService(serviceUrl, data, callback) {
    $.ajax({
        url: serviceUrl,
        data: data,
        dataType: 'json',
        contextType: "application/json", 
        cache: false,
        success: callback,
        error: function (msg) {
            alert("Request Failed!");
        }
    });
}
+3
4

, , JSON, . . JSON, XML JSON. , , JSON, , ResponseFormat WebGet .

JSON, XML, , "application/json", WebOperationContext.OutgoingResponse.Format.

post, . , accept HttpWebRequest "application/json"

+1

, , , Silverlight HttpWebRequest, , text/json, ( ) javascript, , XMLHttpRequest, Silverlight. , .

+1

, , , , .

Content-Type , POST HTTP

HTTP- . GET - . . GET Content-Type, .

Fiddler , WCF . Silverlight, .

: JSON. Accept, , .

wreq.ContentType = "application/json"; 

wreq.Accept = "application/json"; 

JSON .

+1

Can you post the details for the WCF endpoint? If it returns XAML, the only way to get JSON is if it is configured. The initial request simply asks for the URL, it does not indicate "mode", so I assume WCF uses XML by default. There must be either a specific endpoint for JSON responses or a parameter in WCF to only return them.

0
source

Source: https://habr.com/ru/post/1784469/


All Articles