문제

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);
}
도움이 되었습니까?

해결책

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);
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top