Click here to Skip to main content
15,867,594 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Currenlty my send request method returns a string but I would like to know how can I make it to return HttpWebResponse, so I can get the response in my GET, POST calls as Json

Here is my SendRequest method

 public class RestClient{

private static string JSON_ERROR = "error";

private string SendRequest(HttpWebRequest request, string queryParams, string token)
        {
            HttpWebResponse response = null;
            if(token != null)
            {
                request.Headers.Add("Authorization", "Bearer " + token);

            }

            request.ContentType = "application/json";
            
            try
            {
                
                response = (HttpWebResponse)request.GetResponse();
                
            }
            catch (WebException wex)
            {
                if (wex.Response == null)
                return JSON_ERROR;

                using (var errorResponse = (HttpWebResponse)wex.Response)
                {
                    using (var reader = new StreamReader(errorResponse.GetResponseStream()))
                    {
                        return reader.ReadToEnd(); //expected error from JSON
                    }
                }
            }

            var responseVal = new StreamReader(stream: response.GetResponseStream()).ReadToEnd();

            return responseVal;
        }


}

Here is my GET and POST methods

 public string IsGet(string baseURL, string basePath, string queryParams, string token)
        {
            var request = (HttpWebRequest)WebRequest.Create(baseURL+basePath+queryParams);
            request.Method = "GET";
           return SendRequest(request, queryParams, token);
        }


        public string IsPost(string baseURL, string basePath, string queryParams, string json, string token)
        {
            var request = (HttpWebRequest)WebRequest.Create(baseURL+basePath+queryParams);
            request.Method = "POST";
            request.ContentType = "application/json";
            request.Headers.Add("Authorization", "Bearer " + token);
            byte[] postBytes = Encoding.ASCII.GetBytes(json);
            request.ContentLength = postBytes.Length;
            var requestStream = request.GetRequestStream();
            requestStream.Write(postBytes, 0, postBytes.Length);
            requestStream.Close();

            return SendRequest(request, queryParams, token);
        }


What I have tried:

I have tried the following solution but When I try to get my response in my GET or POST call, its null.



private HttpWebResponse SendRequest(HttpWebRequest request, string queryParams, string token)
      {
          HttpWebResponse response = null;
          if(token != null)
          {
              request.Headers.Add("Authorization", "Bearer " + token);

          }

          request.ContentType = "application/json";

          try
          {

              response = (HttpWebResponse)request.GetResponse();

          }
          catch (WebException wex)
          {
              if (wex.Response == null)
              //return JSON_ERROR;

              using (var errorResponse = (HttpWebResponse)wex.Response)
              {
                  using (var reader = new StreamReader(errorResponse.GetResponseStream()))
                  {
                      //return reader.ReadToEnd(); //expected error from JSON
                  }
              }
          }

                             //response.GetResponseStream();
          var responseVal = new StreamReader(stream: response.GetResponseStream()).ReadToEnd();
           var json =  JsonConvert.DeserializeObject(responseVal);

          return json;
      }
Posted
Updated 17-May-20 21:18pm
v3

1 solution

Not sure I fully understand the 'why', but as your question is phrased, if this as you have it returns a string

private string SendRequest(HttpWebRequest request, string queryParams, string token)
    {
        ...
        return someString;
    }


then this
private HttpWebResponse SendRequest(HttpWebRequest request, string queryParams, string token)
    {
        HttpWebResponse response
        ...
        return response;
    }


does what you've asked for.... if that's not what you want, please modify your original question, please don't go posting your answers in the 'solutions'
 
Share this answer
 
Comments
Member 14779968 17-May-20 23:11pm    
Thank Garth. I think my main problem was to convert the response to Json when returning the HttpWebResponse. Here is the main problem I am facing trying to Convert the HttpWebReponse to Json. I opened in another question https://www.codeproject.com/Questions/5268409/How-to-convert-the-response-to-json-using-httpwebr

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900