Mi chiedo se c'è un modo semplice per ottenere la risposta di un httpwebrequest asincrono.Ottenere la risposta di un HttpWebRequest asincrono
Ho già visto questa domanda here ma tutto quello che sto cercando di fare è restituire la risposta (che di solito è json o xml) sotto forma di stringa ad un altro metodo in cui posso quindi analizzarlo/gestirlo di conseguenza.
Heres po 'di codice:
ho questi due metodi statici qui che secondo me sono thread-safe come tutte le params sono passati e non ci sono variabili locali condivisi che utilizzano i metodi?
public static void MakeAsyncRequest(string url, string contentType)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.ContentType = contentType;
request.Method = WebRequestMethods.Http.Get;
request.Timeout = 20000;
request.Proxy = null;
request.BeginGetResponse(new AsyncCallback(ReadCallback), request);
}
private static void ReadCallback(IAsyncResult asyncResult)
{
HttpWebRequest request = (HttpWebRequest)asyncResult.AsyncState;
try
{
using (HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(asyncResult))
{
Stream responseStream = response.GetResponseStream();
using (StreamReader sr = new StreamReader(responseStream))
{
//Need to return this response
string strContent = sr.ReadToEnd();
}
}
manualResetEvent.Set();
}
catch (Exception ex)
{
throw ex;
}
}
Il codice che hai postato lavorato bene una volta ho rimosso il ManualResetEvent estranea.Impostato(); - qual è il problema che stai avendo? –
@JamesManning Ciao sì che era un errore di battitura, io sono dopo un modo più semplice per ottenere il risultato. Quello che hai fornito (compito) è esattamente in linea con le linee. Ho appena fatto il salto da richieste sincrone, sembra che ci sia molto di più in corso. Grazie –
gdp