|
I have a windows forms application where I need to download or get access to a response stream for a requested URL (In my case a gif file). The website uses forms authentication and therefore requires an authentication cookie and no resources on the site can be downloaded without first being redirected to a login page. I found this article Screen Scraping, ViewState, and Authentication using ASP.Net[^] from which I converted C# to VB.NET (the main difference is that the example code is run from an asp.NET application). However when I try to run my code I get a WebException (500) Internal Server Error.
Here is my code, does anyone have any suggestions or hints/another way of doing this?
<br />
Dim strPageURL As String = "http://localhost/MySite/images/uk.gif"<br />
Dim strLoginURL As String = "http://localhost/MySite/login/login.aspx"<br />
<br />
'first, request the login form to get the viewstate value<br />
Dim webReq As HttpWebRequest = DirectCast(WebRequest.Create(strLoginURL), HttpWebRequest)<br />
webReq.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; .NET CLR 1.1.4322)"<br />
<br />
webReq.Credentials = New NetworkCredential(Me.ContainerForm.UserName, Me.ContainerForm.Password)<br />
<br />
Dim stmResponseReader As New StreamReader(webReq.GetResponse.GetResponseStream)<br />
Dim strResponseData As String = stmResponseReader.ReadToEnd<br />
stmResponseReader.Close()<br />
<br />
'extract the viewstate value and build out POST data<br />
Dim strViewState As String = ExtractViewState(strResponseData)<br />
Dim postData As String = [String].Format("__VIEWSTATE={0}&txtUsername={1}&txtPassword={2}&btnLogin=Login", strViewState, Me.ContainerForm.UserName, Me.ContainerForm.Password)<br />
<br />
'have a cookie container ready to receive the forms auth cookie<br />
Dim cookies As New CookieContainer<br />
<br />
'now post to the login form<br />
webReq = DirectCast(WebRequest.Create(strLoginURL), HttpWebRequest)<br />
webReq.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; .NET CLR 1.1.4322)"<br />
webReq.Credentials = New NetworkCredential(Me.ContainerForm.UserName, Me.ContainerForm.Password)<br />
webReq.Method = "POST"<br />
webReq.ContentType = "application/x-www-form-urlencoded"<br />
webReq.CookieContainer = cookies<br />
<br />
'write the form values into the request message<br />
Dim requestWriter As New StreamWriter(webReq.GetRequestStream())<br />
requestWriter.Write(postData)<br />
requestWriter.Close()<br />
<br />
'we don't need the contents of the response, just the cookie it issues<br />
webReq.GetResponse().Close()<br />
<br />
'now we can send out cookie along with a request for the protected page<br />
webReq = DirectCast(WebRequest.Create(strPageURL), HttpWebRequest)<br />
webReq.Credentials = New NetworkCredential(Me.ContainerForm.UserName, Me.ContainerForm.Password)<br />
webReq.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; .NET CLR 1.1.4322)"<br />
webReq.CookieContainer = cookies<br />
<br />
'ERROR OCCURS HERE<br />
Dim stmResponse As Stream = webReq.GetResponse().GetResponseStream()<br />
<br />
Me.PictureBox1.Image = Image.FromStream(stmResponse)<br />
Jim
|
|
|
|
|
I plan to develop a com+ serviced component in Windows 2000 server. Now my requirement is that the deployed serviced component should be available to client applications developed as a Windows Applications (Winforms) and running on Client machines in the network.
ie. How should i access the serviced component in a Windows Application (winforms) ???
I plan to deploy some serviced components, which i want to access from ASP.net applications and also through Windows applications ???
Could anyone help ???
Chandru
|
|
|
|
|
Export the service component installation setup and embed it in desktop setup application. when you application will run it will prompt the user with the server name to provide which you will save in the registry or any ini file. then in the code u have to use
object=createObject("project.class",ServerName)
and it will create the object for that server.
test and let me know if any thing goes wrong.
|
|
|
|
|
could u just brief a little abt the export & embed steps u mentioned in the reply. I'm not clear abt it.
|
|
|
|
|
well do the following
go to control Panel-administrative tools then Component Services.
in Component services create a new Package and add the components in it. Do it this way dont do it through dot net command prompt. now right click on the package and export it. User Server Option in the export with name Suppose "abc.msi". now this abc is the setup of components for you. Go to your VB.Net project. You had already added the deployment project. in this project add this Exe and put the custom event to run the setup during main setup. Now when you will install the package at client.(Assuming Server is already installed on machine X) now on the startup form suppose logon fomr you will check first on load that there is any entry for the server in any text file. if no then prompt the user to enter the server Name and he will enter the name "X" now you will test the connection by creating object as I told you in my previous reply. if the connection goes fine then you are done and if not then let me know again
Best of luck
|
|
|
|
|
thank you for advice. i'm new to serviced components programming. just learning the tricks. Hop u will help me. I understood what u told. But i could not make it into practise.
1. I just created a serviced component class with a public method which returns a string. And registered in the component services. Now according
to the .net principle now if i create a client project and on the form if i add the line "imports SERVERCOMPONENT" the component will be accessible in the form. Is'nt it ... or do i need to add the component by going to the add reference and linking the component ???
I will add the code for your reference ...
ServerLibrary.cs
Imports System.EnterpriseServices
Imports System.Runtime.CompilerServices
Imports System.Reflection
<assembly: applicationname("serverclass")="">
<assembly: assemblykeyfileattribute("d:\netprj\servicedcomponent\serverlibrary\serverclass.snk")="">
Namespace Serverclass
<transaction(transactionoption.required)> _
Public Class ServerClass
Inherits ServicedComponent
<autocomplete()> _
Public Function DoSomething() As String
Return " From a Serviced Component"
End Function
End Class
End Namespace
This class i compile and i'm adding it in the com+ servcies. The component is available in the component services.
And on the same machine i'm adding a client vb.net project .. i'm adding
imports serverclass
or imports serverlibrary.serverclass on the form code
Now the class is not available for instantiation .....
2. As u said in the previous reply after getting the above client ready I have to add a deployment project and then add the component export file to make the client installation file. Is'nt it ...
Chandru
|
|
|
|
|
ok lets get this way.
1. you created a class project which you will deploy on transaction server.
2. In transaction server you will right click on the newly created package (as mentioned in last reply..how to create package)by you and click export. it will create a setup file which will be deployed at the client side which you will embbed in the client setup project.
3. or do not embbed it . run the client setup one one machine and also run this exported setup on that machine one by one.
4, in code of client project you WILL NOT ADD ANY REFERENCE of this project. what you will do is the
Dim obj as object
obj=CreateObject("ProjectName.ClassName",ServerName)
' where servername is the name of the server where transaction server is
this will create an object and after that you will call the fuynction inside obj like
obj.FunctionName(parameters)
best of luck
and let me know if any problem comes
|
|
|
|
|
Now I have created the client application in which i call the following code
in the button click event ...
But an error occurs ...
Microsoft.VisualBasic cannot create ActiveX component. Now why is this happening ????
ServerClass is the serviced component class
chandru is the server name
dosomething is a public method in the component which returns a string.
now i'm testing the client project on the same server machine.i thoght after testing the client on the same server machine, i could then test it on the different machine ..
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim AA As Object
Try
AA = CreateObject("ServerClass", "chandru")
MsgBox(AA.dosomething())
Catch ee As Exception
MsgBox(ee.Source & ee.Message)
End Try
End Sub
Server Component code ....
Imports System.EnterpriseServices
Imports System.Runtime.CompilerServices
Imports System.Reflection
<assembly: applicationname("serverclass")="">
<assembly: assemblykeyfileattribute("d:\netprj\servicedcomponent\serverlibrary\serverclass.snk")="">
Namespace Serverclass
<transaction(transactionoption.required)> _
Public Class ServerClass
Inherits ServicedComponent
<autocomplete()> _
Public Function DoSomething() As String
Return " From a Serviced Component"
End Function
End Class
End Namespace
|
|
|
|
|
AA = CreateObject("ServerClass", "chandru")
this line has problem... it should be like this
AA = CreateObject("ProjectName.ServerClass", "chandru")
and it will start working for you secondly I did not used namespace in my project as I dont know how to create object for that
i did this way
' I did not put require transaction here as I need to commit transaction per
' function instead of per object creation. its all on the requirement
Public Class DC
Inherits EnterpriseServices.ServicedComponent
my email is wet51@hotmail.com (you can add me on messenger if required)
|
|
|
|
|
It works now. That was very nice of you to have me guided like this. Thanks a lot.
I plan put some serviced components on Windows 2000 server. Now some of my client machines on the network has windows 98 os. Will i be able to install the exported serviced component (.msi file) on the client machine ..
Or is there any other mechanism through which i can access the serviced components from a Windows 98 client machine .. ????
chandru
|
|
|
|
|
good news your problem is solved. for 98 I have never run the program. over there, there is no transaction server but there is DCOM which I guess should work. run the MSI on the client 98. if it does not then from Transaction server export the package again but this time choose the client option instead of server.
there is one other way of registering the components on clinet 98. but I have never used that so on 98 you have to explore that.
click start then run and write there
DCOMcnfg
press enter and you will see the window where components are registered.you can manually do this once and then write the script file for 98 windows. so best of luck for 98 as in this windows i am unable to help you more then this.
|
|
|
|
|
if u feel its ok .. pls give me your mail id maybe i could get in touch with u.
|
|
|
|
|
Hi,
I was wondering if there is out there a FREE controls to have your visual application looks like office 2003 (Menu, ToolBar, Shortcut Bar, MDI Tabs,...). If so, can you please help?
Thank you,
|
|
|
|
|
|
Currently I use vbscript to call/run apps and am wondering about an easier to do this. When I first started programming I was using a script that called a batch which then called an app. The script would hide the cmd prompt window but it was a huge hassel
._._._._._.-.-.-.-.-._._._._._.-.-.-.-.-._._._._._.-.-.-.-.-
Spimoles.NET
clean and elegant. a beautiful craft
|
|
|
|
|
Why wouldn't you launch the app directly? There's nothing in a batch file that you couldn't do in script.
RageInTheMachine9532
"...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
|
|
|
|
|
Dave Kreskowiak wrote:
Why wouldn't you launch the app directly? There's nothing in a batch file that you couldn't do in script.
Simply because it was late and I was lazy. Everytime I had tried launching directly it would mess up. So I made a batch that would launch the app and used a script to launch the batch. Yes I know, it didnt have to be that complicated but that was a while back and it was a personal program.
._._._._._.-.-.-.-.-._._._._._.-.-.-.-.-._._._._._.-.-.-.-.-
Spimoles.NET
clean and elegant. a beautiful craft
|
|
|
|
|
It's not different than launching a batch file. What does the code look like when you try and launch the app?
RageInTheMachine9532
"...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
|
|
|
|
|
vb.NET nubie, trying to print information on a form in a report format. I found a piece of code written by "sylvain2222",but I can't seem to duplicate the code in my project, more specifically, I can't understand where this line of code is coming from ..."Dim fp As FormPrinting.FormPrinting"
Here's part of the code...
Public Class DemoForm
Inherits System.Windows.Forms.Form
#Region " Windows Form Designer generated code "
'Create FormPrinting object
Dim fp As FormPrinting.FormPrinting
Private Sub ButtonPrintMe_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonPrintMe.Click
CreateFormPrintingObject(Me)
SetProperties()
' Print!
fp.Print()
Me.ButtonTrace.Enabled = True
End Sub
...more code...
Any help would be appreciated.....
|
|
|
|
|
waingee,
There is a couple of ways to go about it.
First, you can take sylvain2222's FormPrinting.dll, that is found in his demo project, and put it in the bin folder of your app project and then create a reference to it in the solution explorer.
Or Second, you can convert his class (FormPrinting.cs) that is in the FormPrinting project(and the src.zip file), inside his demo project to VB.Net and add it as a class to your App project.
I hope this helps
progload
|
|
|
|
|
Thanks Progload,
Your first suggestion did the trick.
God Bless!
|
|
|
|
|
I have the following problem. I have one form, let's call it FrmBase, I use as a template for almost every form in the system. I inherit one form, let call it FrmIn1, and another, called FrmIn2. I call FrmIn2 from FrmIn1 (using a Button) and when I close FrmIn2, automatically FrmIn1 close too. I have to avoid this behavior. Any suggestions?
|
|
|
|
|
Look at the flow of control in FrmIn1. Does the code behind the button that calls FrmIn2 exit the progeam after FrmIn2 unloads? Possibly both FrmIn1 and FrmIn2 objects are being destroyed. Show us some code and we may be able to help more.
...Steve
|
|
|
|
|
The Ubound function of VB 6.0 is good in my computer.
However, when I install my application on another computer which runs on MS Windows 2000 Advanced Server, the error appears.
Why?
Should I add some dll for the Ubound function?
Thanks!
|
|
|
|
|
UBound doesn't have ANY dependancies on the machine or O/S at all.
The problem MUST be with your code. Perhap's your assuming that an array is being created/populated properly, somewhere else in your code, when it's not?
What's the error? What is the code where the error is occuring?
RageInTheMachine9532
"...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
|
|
|
|