Frage

EDIT: For some reason it all of a sudden works. Didn't change anything in the code..

I'm using a HttpWebRequest to get an Xml file from URL. This the HttpWebRequest works perfectly on all my other pages, but on this one, it doensn't. The text in obj.Text is 4015087900026 (checked it while code is running) and the text of the string url is http://cocktailpws.net23.net/requests/get_cocktail.php?barcode=4015087900026 , but the response is null. No exceptions are thrown or any errors. Does anyone know what I might be doing wrong?

C#:

try
{
    //string urlPar = "barcode=" + obj.Text;
    string url = "http://cocktailpws.net23.net/requests/get_cocktail.php?barcode=" + obj.Text;

    HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
    request.BeginGetResponse(r =>
    {
       var reponse = request.EndGetResponse(r);
       XDocument xDoc = XDocument.Load(reponse.GetResponseStream());
       //rest of the code
    }, null);
}
catch (Exception myExc)
{
       Console.WriteLine(myExc.Message);
}
War es hilfreich?

Lösung

If you want to use HttpClient class, here is a working solution

HttpClient client = new HttpClient();
var xml =  await client.GetStringAsync("http://cocktailpws.net23.net/requests/get_cocktail.php?barcode=4015087900026");
var xDoc = XDocument.Parse(xml);
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top