Solved

VB.NET HttpWebRequest

Posted on 2016-11-23
12
135 Views
Last Modified: 2016-11-28
Hello EE,

In my vb.net page, I do something like this after creating my HttpWebRequest :

                            Using dataStream = requestLoginToPage1.GetRequestStream()
                                dataStream.Write(bytes1, 0, bytes1.Length)
                                dataStream.Close()
                            End Using

Open in new window


My question is, by doing that, I saw that this code slows down ALOT my script. If I remove it just for fun and run, it's extremely fast.
So, in other words, is there a way to use "getRequestStream" or similar and yet still have the speed or its the only way ?
0
Comment
Question by:PhilippeRenaud
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 3
  • 2
12 Comments
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 41899979
0
 
LVL 1

Author Comment

by:PhilippeRenaud
ID: 41900524
im trying to do with Await and lambda has i saw an example over the internet.

                            Using requestStream = New BinaryWriter(Function()
                                                                       Await requestLoginToPage1.GetRequestStreamAsync()
                                                                   End Function)
                                requestStream.Write(bytes1, 0, bytes1.Length)
                            End Using

Open in new window


but what am i missing, not sure to fix the error saying :

Lambda expression cannot be convert to IO.Stream,not a delegate type

how would you write it ?
0
 
LVL 1

Author Comment

by:PhilippeRenaud
ID: 41900529
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 70

Accepted Solution

by:
Éric Moreau earned 250 total points
ID: 41900552
I don't have a sample anywhere. Have you seen https://www.thomaslevesque.com/tag/timeout/
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 41900648
The error is because this is what you're passing to BinaryWriter:

Function()
    ...
End Function

...not this:

Await requestLoginToPage1.GetRequestStreamAsync()

You're passing the labmda itself, not it's reasult. Just await the GetRequestStreamAsync directly:

Using requestStream = New BinaryWriter(Await requestLoginToPage1.GetRequestStreamAsync())
    requestStream.Write(bytes1, 0, bytes1.Length)
End Using

Open in new window


GetRequestStreamAsync is already awaitable (because it returns Task(n)); you don't have to wrap it in a lambda.
0
 
LVL 1

Author Comment

by:PhilippeRenaud
ID: 41901769
Kaufmed, hold on so you mean, I only have to write what you wrote and should be fine ?

if I only do this :

                            Using requestStream = New BinaryWriter(Await requestLoginToPage1.GetRequestStreamAsync())
                                requestStream.Write(bytes1, 0, bytes1.Length)
                            End Using

Open in new window


I have an error saying :

Await can only be used within an async lambda blablabla...
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 41901977
OK, my mistake--kind of. You'd have to mark your outer method as Async (because you're using Await).

e.g.

Async Sub YourMethod()
    Using requestStream = New BinaryWriter(Await requestLoginToPage1.GetRequestStreamAsync())
        requestStream.Write(bytes1, 0, bytes1.Length)
    End Using
End Sub

Open in new window


But the implication of doing that is that you'd probably have to continue marking methods Async and Awaiting the results of such methods all the way up the chain. That's probably more work than you want for this.


What kind of project is this:  Console? Library? Win Forms? ASP.NET?
0
 
LVL 1

Author Comment

by:PhilippeRenaud
ID: 41901981
its vb.net  win forms ....

i tried liek you said and i have no errors now... but my only problem is that... This USING block is inside a list.Forach(Async Sub(z) ...

and for some reason, it does not loop, it stops after only 1 value.  (I have 36 values in my list) after index 0 it does not loop
but there are no error poping

you think its because of those Asyn calls ?
0
 
LVL 1

Author Comment

by:PhilippeRenaud
ID: 41902346
ok i found my problem
0
 
LVL 1

Author Comment

by:PhilippeRenaud
ID: 41902347
im going to give points but, i did not really see any speed up by using Async ...
its really slow I find....

like almost a second per item in my list.
i dont have enough knowledge to know if its normal
0
 
LVL 75

Assisted Solution

by:käµfm³d 👽
käµfm³d   👽 earned 250 total points
ID: 41902395
Async isn't intended to make anything faster. It simply allows your code to not block on I/O work. While the system is doing I/O, there is nothing meaningful that the CPU can be doing. Async allows the CPU to continue processing other work while waiting for the I/O to complete. It's really intended for a more responsive UI.
0
 
LVL 1

Author Comment

by:PhilippeRenaud
ID: 41904880
For you Kaufmed and Eric in case you know an answer :

https://www.experts-exchange.com/questions/28985931/Speeding-up-a-Loop.html
0

Featured Post

Enroll in June's Course of the Month

June’s Course of the Month is now available! Experts Exchange’s Premium Members, Team Accounts, and Qualified Experts have access to a complimentary course each month as part of their membership—an extra way to sharpen your skills and increase training.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question