Readdy Write  
0,00 €
Your View Money
Views: Count
Self 20% 0
Your Content 60% 0

Users by Links 0
u1*(Content+Views) 10% 0
Follow-Follower 0
s2*(Income) 5% 0

Count
Followers 0
Login Register as User

UWP WebRequest: Problem reading a webpage

02.07.2018 (👁7655)


 

Under Windows, you can access a web page in two ways.

There is the older WebRequest and the newer HttpClient.

 

WPF often uses the WebRequest because this access also allows ftp pages. Unfortunately, when switching from WPF to UWP the WebRequest hangs very often and often causes problems.

 

 

1) UWP web request: synchronous access

Error Description:

When accessing a web page with WebRequest, the error message appears in synchronous mode:

The operation was canceled

 

Reason:

Initially unknown ... however it probably indicates a redirect of the website. For example, if a URL ends with the /, the web pages internally redirect the page for legibility.

/

 

 

?

Name

Value

Type

ex

{System.OperationCanceledException: The operation was canceled. at System.Net.HttpWebRequest.GetResponse() at robot_ComputerFutures.MainPage.<Web_Get_HtmlDocument>d__6.MoveNext()}

System.Exception {System.OperationCanceledException}

CancellationToken

IsCancellationRequested = true

System.Threading.CancellationToken

Data

{System.Collections.ListDictionaryInternal}

System.Collections.IDictionary {System.Collections.ListDictionaryInternal}

HResult

-2146233029

int

HelpLink

null

string

InnerException

null

System.Exception

Message

"The operation was canceled."

string

Source

"System.Net.Requests"

string

StackTrace

" at System.Net.HttpWebRequest.GetResponse()\r\n at robot_ComputerFutures.MainPage.<Web_Get_HtmlDocument>d__6.MoveNext()"

string

TargetSite

{System.Net.WebResponse GetResponse()}

System.Reflection.MethodBase {System.Reflection.RuntimeMethodInfo}

 

2) UWP web request: asynchronous access

objRequest.GetResponseAsync

When using the asynchronous method, the cursor jumps between the two calls in the loop and obstructs the application.

//* get the HTML Document of a website-URL      

try

{

    //-< init >-

    //< WebRequest and Response >

    WebRequest objRequest = WebRequest.Create("https://www.computerfutures.com/de/job/Project-Manager-39");

    objRequest.Timeout = 15;

    WebResponse objResponse = null;

    try

    {

        objResponse = await objRequest.GetResponseAsync(); 

    }

    catch (Exception ex)

    {

        clsSys.show_Message(ex.Message);

        return null;                    

    }

    

    //</ WebRequest and Response >

 

    //< Stream and Reader >

    Stream objDataStream = objResponse.GetResponseStream();

    StreamReader TextReader = new StreamReader(objDataStream);

    //</ Stream and Reader >

    //-</ init >-

 

 

 

Problem:

3) UWP: webRequest-> setting redirect

AllowAutoRedirect in UWP

Cause seems to be that the website makes a redirect

However, the WebRequest in UWP no longer has any setting for the behavior of Redirect

//< WebRequest and Response >

sURL = "https://www.computerfutures.com/de/job/Project-Manager-39";

WebRequest objRequest = WebRequest.Create(sURL);

objRequest.AllowAutoRedirect = true; //*not in UWP

objRequest.Timeout = 15;

WebResponse objResponse = null;

try

{

    objResponse = await objRequest.GetResponseAsync(); 

}

catch (Exception ex)

{

    clsSys.show_Message(ex.Message);

    return null;                    

}

 

 

 

UWP: Universal Windows Platform

WPF: Windows Presentation Foundation