Solved

C# web post form values to login and redirect

Posted on 2014-10-23
10
333 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
[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
  • 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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
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

Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

Question has a verified solution.

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

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
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.
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …

705 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