Solved

C# web post form values to login and redirect

Posted on 2014-10-23
10
307 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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

809 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