Domanda

Stavo cercando di utilizzare HttpWebRebreQuest per utilizzare un servizio come il servizio su un server remoto e dalla prima esecuzione stessa, il mio codice è stato appeso al programma.Poi l'ho provato come un'applicazione console per assicurarmi che non abbia nulla a che fare con il programma stesso ma senza fortuna!

        string credentialsJson = @"{""username"":""test"",
                                      ""password"":""test"" 
                                   }";

        int tmp = ServicePointManager.DefaultConnectionLimit;
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(@"https://qrua.com/qr/service" + @"/auth/login");
        request.Method = "POST";
        request.KeepAlive = true;
        request.Timeout = 50000 ;
        request.CookieContainer = new CookieContainer();
        request.ContentType = "application/json";
        try
        {
            StreamWriter writer = new StreamWriter(request.GetRequestStream());
            writer.Write(credentialsJson);
        }
        catch (Exception e)
        {
            Console.WriteLine("EXCEPTION:" + e.Message);
        }

        //WebResponse response = request.GetResponse();
        try
        {
            using (WebResponse response = (HttpWebResponse)request.GetResponse())
            {
                Console.WriteLine("request:\n" + request.ToString() + "\nresponse:\n" + response.ContentLength);
                response.Close();
            }
        }
        catch (Exception e)
        {
            Console.WriteLine("EXCEPTION: in sending http request:" + " \nError message:" + e.Message);
        }
.

ha provato diverse cose da forum diversi ma non aiuta.Anche una semplice app console con il codice sopra pende la console indefinitamente!Qualsiasi aiuto sarebbe fantastico ..

Grazie

È stato utile?

Soluzione

Non stai mai chiudendo il StreamWriter ... quindi sospetto che non sia fluso.Certamente ti aspetterei un errore dal server invece di un semplice appendere, ma vale la pena guardare.

BTW, non è necessario chiudere la risposta e smaltirlo.Solo la Dichiarazione using è sufficiente.

Altri suggerimenti

Non c'è molto possibile se il server remoto non risponde a parte diverso dalla definizione di un Timeout e catturare l'eccezione come hai fatto per informare l'utente che l'operazione non può completare perché il sito remoto non ha risposto:

.
var request = (HttpWebRequest)WebRequest.Create("https://qrua.com/qr/service/auth/login");
request.Timeout = 5000;
// If the server doesn't respond within 5 seconds you might catch the timeout exception
using (var response = request.GetResponse())
{

}
.

Se non si desidera congelare l'interfaccia utente è possibile utilizzare la versione ASYNC: begingetresponse

Prova a specificare la richiesta.ContentLength.

Prima di fare:

        StreamWriter writer = new StreamWriter(request.GetRequestStream());
        writer.Write(credentialsJson);
.

Prova qualcosa del genere:

using (MemoryStream stream = new MemoryStream())
{
    using (var writer = StreamWriter writer = new StreamWriter(stream))
    {
        writer.Write(credentialsJson);
        writer.Close();
    }
    request.ContentLength = stream.Length;
}
.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top