Convert Windows application to Windows Phone - C# (Httpwebresponse and Httpwebrequest)

Asked

Viewed 127 times

1

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

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

I will show the captcha to the user and I will 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;
        }
    }

But Visual Studio is accusing that httpWebRequest.GetResponseStream() there is no.

What other way to do this or correct the current?

1 answer

2

The methods of network access on the Windows Phone platform are all asynchronous - you’ll 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 the use of the library HttpClient, which supports the standard of Tasks, where you can use the await not to 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();
        // ...
    }
  • I will test as soon as I get home and put the result. Thank you

Browser other questions tagged

You are not signed in. Login or sign up in order to post.