简体   繁体   中英

HttpWebRequest.getResponse() returning NULL

I am attempting to create a console app that sends a WebRequest to a website so that I can get some information back from it in JSON format. Once I build up the request and try to get response I just want to simply print out the data, but when I call httpWebRequest.getResponse() it returns NULL.

I have tried multiple other methods of sending the data to the the url but those are all giving me like 404, or 400 errors, etc. This method at least isn't giving me any error, just a NULL.

Here is a snapshot of the documentation I am using for the API (albeit the docs aren't complete yet):

Here is the console app code that I have right now:

    try
        {
            var httpWebRequest = (HttpWebRequest)WebRequest.Create("https://api.remot3.it/apv/v27/user/login");
            httpWebRequest.ContentType = "application/json";
            httpWebRequest.Method = "POST";
            httpWebRequest.Headers.Add("developerkey", "***KEY***");
            using (var streamWriter = new

            StreamWriter(httpWebRequest.GetRequestStream()))
            {
                string json = new JavaScriptSerializer().Serialize(new
                {
                    email = "***EMAIL***",
                    password = "***PASSWORD***"
                });
                Console.WriteLine(json);
                streamWriter.Write(json);
            }
            var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
            using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
            {
                var result = streamReader.ReadToEnd();
                Console.WriteLine(result);
                Console.ReadLine();
            }
        }catch(Exception e)
        {
            Console.WriteLine(e.Message);
            Console.WriteLine(e.StackTrace);
            Console.ReadLine();
        }

Expected output is some JSON data, but I am getting a NULL back from getResponse().

Try to serialize the credential in your form and for header send as parameter for this class. Check below for my code. It is not 100 % fit to your requirement, but atleast it will help to get through your logic.

Here is what I get Json Response from this code. Its work Perfect. Please remember to add timeout option on your webrequest and at the end close the streamreader and stream after completing your task. please check this code.

  public static string httpPost(string url, string json)
    {
        string content = "";          
        byte[] bs;

        if (json != null && json != string.Empty)
        {
            bs = Encoding.UTF8.GetBytes(json);
        }
        else
        {
            bs = Encoding.UTF8.GetBytes(url);
        }
        HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(url);
        req.Method = "POST";

        if (json != string.Empty)
            req.ContentType = "application/json";
        else
            req.ContentType = "application/x-www-form-urlencoded";
        req.KeepAlive = false;
        req.Timeout = 30000;
        req.ReadWriteTimeout = 30000;
        //req.UserAgent = "test.net";
        req.Accept = "application/json";
        req.ContentLength = bs.Length;
        using (Stream reqStream = req.GetRequestStream())
        {
            reqStream.Write(bs, 0, bs.Length);
            reqStream.Flush();
            reqStream.Close();
        }
        using (WebResponse wr = req.GetResponse())
        {
            Stream s = wr.GetResponseStream();
            StreamReader reader = new StreamReader(s, Encoding.UTF8);
            content = reader.ReadToEnd();
            wr.Close();
            s.Close();
            reader.Close();
        }
        return content;

    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM