Solved

C# web post form values to login and redirect

Posted on 2014-10-23
10
322 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
Technology Partners: 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 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

Technology Partners: 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!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
transition to visual .net from vb6 5 62
Securing WEBAPI on Azure 2 26
parsing question on dynamic concatenation 3 34
Nested forach loop to linq 3 28
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
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.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

749 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