Search code examples
c#jsonweb-servicesrestinternal-server-error

Posting a json to a distant server(REST)


I want to send some json data using this code to a distant server (Rest, outside my control), following the way I'm sending it :

I create the url and the request method first :

        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(gUrlDot);
        request.Method = "POST";
        Dictionary<String, String> lDictionary = new Dictionary<String, String>();
        System.Text.UTF8Encoding encoding = new System.Text.UTF8Encoding();
        Dot lDot= new Dot();
        serviceContext lserviceContext = new serviceContext();
        items litems = new items();
        lDot.rad = pClient;
        lDictionary.Add("companyId", "00230");
        lDictionary.Add("treatmentDate", lGlobals.FormatDateYYYYMMDD());
        lDictionary.Add("country", "FR");
        lDictionary.Add("language", "fr");
        lDictionary.Add("Id", "test");


        litems.contextItem = lDictionary;
        lDot.serviceContext = lserviceContext;
        lDot.serviceContext.items = litems;
        String Input=_Tools.JsonSerializer(lDot);
        log.Debug("Input Dot " + Input);

        Byte[] byteArray = encoding.GetBytes(Input);

        request.ContentLength = byteArray.Length;
        request.ContentType = @"application/json";

        using (Stream dataStream = request.GetRequestStream())
        {
            dataStream.Write(byteArray, 0, byteArray.Length);
        }

        long length = 0;

When i'm getting here it crashes with an execption : Error 500 !

        try
        {
          using (var response = (HttpWebResponse)request.GetResponse())
            {
                length = response.ContentLength;
                string output = response.ToString();
                lTrace.AppendLine("-Flux Json recu => " + response.StatusCode + "  " + length);
                log.Debug("Output Dot " + output);

            }
           log.Info(LogsHelper.LogHeader("End processing Get list  ", pClient, Service.SrvGetList, "", lResponse.StatusCode, lResponse.StatusLabel, lResponse.ResponseObject, ref lTrace));

        }
        catch (Exception ex)
        {
            lResponse.StatusCode = StatusCodes.ERROR_COMMUNICATION;
            log.Error(LogsHelper.LogHeader("End processing Get list", pClient, Service.SrvGetList, "", lResponse.StatusCode, ex.Message, lResponse.ResponseObject, ref lTrace));
        }

        return lResponse;
    }

what am i missing here?


Solution

  • A 500 error means there's a problem on the server you're making the request to. You'll need to check to make sure two things;

    1) Make sure your request is properly formatted, and doesn't have any surprises or invalid values for the requested resource.

    2) that the server you want to talk to CAN get requests, and that its up-to-date (if you control the client).

    In either case, the most common reason for a 500 is that something outside of your control has gone wrong on the client server.