0

I have the following function Handle_Log_Clicked() which calls async Task CreateLogCall which makes an HTTP POST request using the repository.CREATE() method and returns HttpResponseMessage type as return value :

I have to use the response in Handle_Log_Clicked() method to retrieve Headers from the HttpResponseMessage response that I get from CreateLogCall ().

In the below code the response does not get set in HttpResponseMessage response .

When I am trying to take a substring of the response resulting in an error.

async void Handle_Log_Clicked(object sender, System.EventArgs e)
    {
        try
        {
            HttpResponseMessage response = await CreateLogCall();
            Debug.WriteLine("Response: ", response);
            var resultHeader = response.Headers.Location.ToString();
            string relatedCallID = resultHeader.Substring(resultHeader.IndexOf('('), resultHeader.Length -1 );

            await CreateLogCallProducts(relatedCallID);

            if (!response.IsSuccessStatusCode)
                await DisplayAlert("Failure", "The data could not be saved", "Ok");
            else
                await DisplayAlert("Success", "The call is logged successfully", "Ok");
        }
        catch (Exception err)
        {
            await DisplayAlert("Error", "Please fill all the details", "Ok");
            Debug.WriteLine(err.Message);
        }
    }

public async Task<HttpResponseMessage> CreateLogCall()
    {
        HttpResponseMessage result = null;
        try
        {
            if (isPrescriber)
            {
                Debug.WriteLine("inside prescriber calls");
                result = await repository.Create(GlobalVariables.AuthToken, "xxxx", logCall);
            }
            else
            {

                result = await repository.Create(GlobalVariables.AuthToken, "yyyyy", logCall);
            }

            return result;
        }
        catch (Exception e)
        {
            Debug.WriteLine(e.Message);
        }
        return result;
    }

Inside repository.cs

public async Task<HttpResponseMessage> Create(string accessToken, string entity, JObject data)
    {
            // The URL for the OData organization web service.
            string url = GlobalVariables.RootUrl + GlobalVariables.EndPoint + entity;

            var content = new StringContent(data.ToString(), Encoding.UTF8, "application/json");
            // Build and send the HTTP request.
            HttpClient httpClient = new HttpClient();
            httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);

            // Wait for the web service response.
            HttpResponseMessage response;
            response = await httpClient.PostAsync(url, content);
            Debug.WriteLine(response);
            return response;
    }

While putting the debugger I can see the response value being pass to Handle_Log_Clicked() but when the code reaches the substring method it throws error :

Index and length must refer to a location within the string. Parameter name: length

Also the code : Debug.WriteLine("Response: ", response); does not print the response but debugger shows value in it.

souravlahoti
  • 716
  • 2
  • 8
  • 29
  • 1
    For substring you're giving an invalid second value. It's not the end of the substring, it's the length. So if ( is not the first character the length is more than there is in the string – Sami Kuhmonen May 19 '17 at 18:21
  • that is also not the right syntax for Debug.WriteLine - you are passing a parameter list without specifying placeholders in your string – Jason May 19 '17 at 18:31
  • @SamiKuhmonen rectified that I think that was the mistake for the error. But I still don't get why didn't `Debug.WriteLine("Response: ", response.Headers.Location.LocalPath.ToString()) ` @Jason (rectiifed the debug.writeline code) do not print the value in the log inside `Handle_Log_Clicked ()` but the same `Debug.WriteLine(response);` inside `Create()` prints the data. – souravlahoti May 19 '17 at 18:47
  • your syntax is still wrong, try: Debug.WriteLine("Response: {0}",response); – Jason May 19 '17 at 22:53
  • There are two ways your call to `Substring()` could go wrong: there is no `'('` character in the string, so the index value is wrong, or you try to get too many characters, making the length value wrong. The exception message clearly says the `length` is wrong, so it's the latter (but you should also prepare for the former). As noted in the first comment above, passing `Length - 1` is wrong; that would only be a valid value in the index value were `0` or `1`. More likely, you just want the remainder of the string, in which case you don't even need to pass the second parameter. – Peter Duniho May 19 '17 at 23:00

0 Answers0