Convert Windows application to Windows Phone - C # (HttpWebResponse and HttpWebRequest)

1

I have a class in C # for Windows that works normally, but I would like it to work on Windows Phone.

The idea is to use the HTTP methods get and post of a web page that has CAPTCHA.

I will show the captcha to the user and I will pass as post the answer.

<input type=hidden id=viewstate name=viewstate value='";
        String StrImagemCaptcha = "<img border='0' id='imgcaptcha' alt='Imagem com os caracteres anti rob";
        String UrlImagemCaptcha = "";
        HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(UrlBase + UrlGet);
        httpWebRequest.CookieContainer = cookieContainer;
        httpWebRequest.ContentType = "application/x-www-form-urlencoded";
        httpWebRequest.Method = "GET";
        httpWebRequest.AllowAutoRedirect = false;
        //httpWebRequest.Timeout = 20000;
        try
        {
            StreamReader stHtml = new StreamReader(httpWebRequest.GetResponseStream(), Encoding.GetEncoding("ISO-8859-1"));
            HtmlResponse = stHtml.ReadToEnd();
            stHtml.Close();
            viewState = HtmlResponse;
            PosString = viewState.IndexOf(StrViewState);
            if (PosString >= 0)
                viewState = viewState.Substring(PosString + StrViewState.Length);
            PosString = viewState.IndexOf("'>");
            if (PosString >= 0)
                viewState = viewState.Substring(0, PosString);
            UrlImagemCaptcha = HtmlResponse;
            PosString = UrlImagemCaptcha.IndexOf(StrImagemCaptcha);
            if (PosString >= 0)
                UrlImagemCaptcha = UrlImagemCaptcha.Substring(PosString + 8 + StrImagemCaptcha.Length);
            PosString = UrlImagemCaptcha.IndexOf("'>");
            if (PosString >= 0)
                UrlImagemCaptcha = UrlImagemCaptcha.Substring(0, PosString);
            UrlImagemCaptcha = UrlImagemCaptcha.Replace("amp;", "");
        }
        catch (Exception ex)
        {
            _erro = ex.Message;
        }
        try
        {
            if (UrlImagemCaptcha.Length > 0)
                return UrlDominio + UrlImagemCaptcha;
            //return new System.Drawing.Bitmap(new System.IO.MemoryStream(new System.Net.WebClient().DownloadData(UrlDominio + UrlImagemCaptcha)));
            else
                return null;
        }
        catch (Exception ex)
        {
            _erro = ex.Message;
            return null;
        }
    }

However Visual Studio is accusing that httpWebRequest.GetResponseStream() does not exist.

What other way to do this or fix the current one?

    
asked by anonymous 30.06.2014 / 04:49

1 answer

2

The network access methods on the Windows Phone platform are all asynchronous - you will have to use the asynchronous version of GetResponse , as in the example below:

    public void Foo()
    {
        var UrlBase = "";
        var UrlGet = "";
        HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(UrlBase + UrlGet);
        httpWebRequest.Method = "GET";
        httpWebRequest.BeginGetResponse(GetResponseCallback, httpWebRequest);
    }

    void GetResponseCallback(IAsyncResult asyncResult)
    {
        HttpWebRequest httpWebRequest = (HttpWebRequest)asyncResult.AsyncState;
        HttpWebResponse response = (HttpWebResponse)httpWebRequest.EndGetResponse(asyncResult);
        StreamReader stHtml = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding("ISO-8859-1"));
        var HtmlResponse = stHtml.ReadToEnd();
        // ...
    }

Another alternative is to use the HttpClient library, which supports the Task pattern, where you can use await so you do not have to "break" the code in different methods:

    public async Task Foo()
    {
        var UrlBase = "";
        var UrlGet = "";
        var client = new HttpClient();
        var response = await client.GetAsync(UrlBase + UrlGet);
        var responseStream = await response.Content.ReadAsStreamAsync();
        var stHtml = new StreamReader(responseStream, Encoding.GetEncoding("ISO-8859-1"));
        var HtmlResponse = await stHtml.ReadToEndAsync();
        // ...
    }
    
30.06.2014 / 22:46