Solved

C# web post form values to login and redirect

Posted on 2014-10-23
10
314 Views
Last Modified: 2015-04-06
Hello experts,

I'm submitting a web form from a C# Windows program.
The goal is to perform a login by post, after login the webserver automatically redirects to page i want, and gives me the html content as the response i need into a string variable.

I use this code to submit to a form to a normal webserver, namely an Apache to a php page, and works (but not login in, just web form returning a response).
But in this case is a little different. The web server is an Lexmark printer that as is own webpage with reports of the tonners and so on.
If i use the regular DownloadString code with the printer, it works fine, i get the response:
WebClient webClient         = new WebClient();
responsefromserver = webClient.DownloadString(URL_PRINTER);

Open in new window

But some printers are protected with password.
So, for those i try this:
NameValueCollection formDataPRINTER = new NameValueCollection();
byte[] responseBytesPRINTER;

formDataPRINTER["login_type"] = "password_only";
formDataPRINTER["goto"] = "/cgi-bin/dynamic/printer/config/reports/devicestatistics.html";
formDataPRINTER["accid"] = "13";
formDataPRINTER["password"] = "123456";

responseBytesPRINTER = webClient.UploadValues(URL_PRINTER_LOGIN, "POST", formDataPRINTER);

Open in new window

After the NameValueCollection values are posted to the web page, she should just accept the values login and redirect to "goto" webpage value from the NameValueCollection.

When the debug gets to last line "responseBytesPRINTER = webClient.UploadValues(..." first i receive this error: "_COMPlusExceptionCode = -532459699" saying "The underlying connection was closed: The connection was closed unexpectedly"

Further, analyzing  the error i get more messages like this:

StackTrace      "   em System.Net.WebClient.UploadValues(Uri address, String method, NameValueCollection data)\r\n   em PrinterInfoToRACC.Form1.Form1_Shown(Object sender, EventArgs e) em d:\\Dropbox\\PROJECTOS\\Visual Studio 2013\\Projects\\PrinterInfoToRACC\\PrinterInfoToRACC\\Form1.cs:line 229"      string

m_InternalStatus      Cannot dereference expression. The pointer is not valid.      System.Net.WebExceptionInternalStatus


What am i missing here? :\

Thx in advanced,
Miguel Guerreiro
0
Comment
Question by:justaphase
  • 5
  • 5
10 Comments
 
LVL 25

Expert Comment

by:apeter
ID: 40402271
Does that look for "contentType" header value in your post request?
0
 
LVL 1

Author Comment

by:justaphase
ID: 40402498
Sorry Peter... i do not follow..
0
 
LVL 25

Expert Comment

by:apeter
ID: 40403344
Talking about this request property, http://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.contenttype(v=vs.110).aspx.

can we know which line is throwing error ?
0
How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

 
LVL 1

Author Comment

by:justaphase
ID: 40406174
I'm not setting any ContentType :\
I should, shouldn't I?..
0
 
LVL 25

Expert Comment

by:apeter
ID: 40406284
Depends on the server, It tells the server what type of data it is coming in. Did you try setting contectType?  Can we see some code and which line is throwing error ?
0
 
LVL 1

Author Comment

by:justaphase
ID: 40406584
The code i use to send the printer the post values i already put in the beginning of this question:
NameValueCollection formDataPRINTER = new NameValueCollection();
byte[] responseBytesPRINTER;

formDataPRINTER["login_type"] = "password_only";
formDataPRINTER["goto"] = "/cgi-bin/dynamic/printer/config/reports/devicestatistics.html";
formDataPRINTER["accid"] = "13";
formDataPRINTER["password"] = "123456";

responseBytesPRINTER = webClient.UploadValues(URL_PRINTER_LOGIN, "POST", formDataPRINTER);

Open in new window


That's what i use to submit the post, and the error comes in the last line "responseBytesPRINTER = webClient.UploadValues(URL_PRINTER_LOGIN, "POST", formDataPRINTER);"

:)
0
 
LVL 25

Expert Comment

by:apeter
ID: 40408273
Does the printer has manual ? It should contain the details for you,
0
 
LVL 1

Author Comment

by:justaphase
ID: 40417087
Manual?... What do you mean? a control panel to go to and get a info i want?
0
 
LVL 25

Accepted Solution

by:
apeter earned 500 total points
ID: 40417326
An api book for the printer which has technical details. Which URL you have hit, whether you need to password, how you have to post....
0
 
LVL 1

Author Comment

by:justaphase
ID: 40418919
There's no need for that.
I know exactly what url to hit. When i do it by the browser everything works fine.
I put the user and password password in specific page that when validate, redirects to the page i want.

I have my app working ok when the printer is set not to use user and pass. i just go directly to the desired page and extract the html report i want.

The problem is when user and pass is required..
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

856 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