1

Currently I try to developed application by using SharePoint that retrieve data from List

Now I try to developed for Login Function, The error display as Below

The underlying connection was closed: An unexpected error occurred on a send.

Here with my Source Code

HttpWebRequest endpointRequest =(HttpWebRequest)HttpWebRequest.Create("https://mvponduty.sharepoint.com/sites/devspace/_api/web/Lists");
            endpointRequest.Method = "GET";
            endpointRequest.Accept = "application/json;odata=verbose";
            endpointRequest.Headers.Add("Authorization",
              "Bearer " + tokenkey.ToString());
            HttpWebResponse endpointResponse =(HttpWebResponse)endpointRequest.GetResponse();

            lblResult.Text = endpointResponse.ToString();
0

If you simply retrieve the lists using SharePoint Rest API, I suggest using HttpClient instead. It's the newest/modern API that supports asynchronous http requests. Your HttpWebRequest may have involved some disposing issue but I can't prove.

Try this:

using (var httpClient = new HttpClient())
{
    httpClient.DefaultRequestHeaders.Add("Accept", "application/json;odata=verbose;charset=utf-8");
    HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, "http://<site url>/_api/web/lists");
    request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", "your_token");
    try
    {
        var response = httpClient.SendAsync(request).Result;
        response.EnsureSuccessStatusCode(); // <= this will throw an exceception if status code is not within 200 - 209
        // Do whatever with the response object.
        // To get the response content:
        var content = response.Content.ReadAsStringAsync().Result;
        JObject json = JObject.Parse(content);
        // Or simply display the status code:
        // lblResult.Text = response.StatusCode;

    }
    catch (Exception ex)
    {
        lblResult.Text = ex.Message;
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.