Solved

C# web post form values to login and redirect

Posted on 2014-10-23
10
290 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
 
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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

746 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now