Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Friday, August 19, 2016 1:41 PM
I have written the below code to login a web page and receive a Token as a response in text format. I am unable to recieve the response from my below code. Can some one please guide me where i have gone wrong.
string pageSource;
string formUrl = "https://mat.medicaleguides.com/mobile_login"; // NOTE: This is the URL the form POSTs to, not the URL of the form (you can find this in the "action" attribute of the HTML's form tag
string formParams = string.Format("Username={0}&password={1}", "my Login name", "my password");
string cookieHeader;
WebRequest req = WebRequest.Create(formUrl);
req.ContentType = "application/x-www-form-urlencoded";
req.Method = "POST";
byte[] bytes = Encoding.ASCII.GetBytes(formParams);
req.ContentLength = bytes.Length;
try
{
MessageBox.Show("Inside Try");
using (Stream os = req.GetRequestStream())
{
MessageBox.Show("Inside GetRequestStream");
os.Write(bytes, 0, bytes.Length);
}
WebResponse resp = req.GetResponse();
cookieHeader = resp.Headers["Set-cookie"];
using (StreamReader sr = new StreamReader(resp.GetResponseStream()))
{
pageSource = sr.ReadToEnd();
MessageBox.Show(pageSource + "--");
}
}
catch (WebException e)
{
using (WebResponse response = e.Response)
{
MessageBox.Show("Inside Catch");
HttpWebResponse httpResponse = (HttpWebResponse)response;
Console.WriteLine("Error code: {0}", httpResponse.StatusCode);
using (Stream data = response.GetResponseStream())
using (var reader = new StreamReader(data))
{
string text = reader.ReadToEnd();
MessageBox.Show(text);
Console.WriteLine(text);
}
}
}
All replies (3)
Thursday, September 1, 2016 9:14 AM âś…Answered | 1 vote
Hi Ranjitha Arjun,
Thank you for posting here.
>> I am unable to recieve the response from my below code.
You could get the stream containing response data sent by the server, call the GetResponseStream method of the WebResponse.
Stream data = response.GetResponseStream;
Here is an example.
using System;
using System.IO;
using System.Net;
using System.Text;
namespace Examples.System.Net
{
public class WebRequestPostExample
{
public static void Main ()
{
// Create a request using a URL that can receive a post.
WebRequest request = WebRequest.Create ("http://www.contoso.com/PostAccepter.aspx ");
// Set the Method property of the request to POST.
request.Method = "POST";
// Create POST data and convert it to a byte array.
string postData = "This is a test that posts this string to a Web server.";
byte[] byteArray = Encoding.UTF8.GetBytes (postData);
// Set the ContentType property of the WebRequest.
request.ContentType = "application/x-www-form-urlencoded";
// Set the ContentLength property of the WebRequest.
request.ContentLength = byteArray.Length;
// Get the request stream.
Stream dataStream = request.GetRequestStream ();
// Write the data to the request stream.
dataStream.Write (byteArray, 0, byteArray.Length);
// Close the Stream object.
dataStream.Close ();
// Get the response.
WebResponse response = request.GetResponse ();
// Display the status.
Console.WriteLine (((HttpWebResponse)response).StatusDescription);
// Get the stream containing content returned by the server.
dataStream = response.GetResponseStream ();
// Open the stream using a StreamReader for easy access.
StreamReader reader = new StreamReader (dataStream);
// Read the content.
string responseFromServer = reader.ReadToEnd ();
// Display the content.
Console.WriteLine (responseFromServer);
// Clean up the streams.
reader.Close ();
dataStream.Close ();
response.Close ();
}
}
}
For more details, please refer to the link.
I hope this would be helpful to you.
If you have something else, please feel free to contact us.
Best Regards,
Wendy
Friday, August 19, 2016 8:24 PM
Hello
And what's the relation to SSIS = SQL Server Integration Services, the topic of this forum; I don't see none?
Olaf Helper
Thursday, September 1, 2016 3:03 PM
You posted code but didn't clarify what problem you're having. Is it not working? If so then have you confirmed that the data you're sending is correct?
Also, I would recommend using WebClient over WebRequest. It provides helper methods to reduce most of your code down to a line or to. For example to post a form to a server you should just be able to call UploadData. If you need to parse HTML responses then consider using something like HtmlAgilityPack to simplify the effort.
For security related stuff you're going to have to analyze how the data is being passed back and forth to the site. In ASP.NET apps this is generally done using a cookie (which you have to attach to each request) and generally a session value in the form data. MVC-apps would generally just use an auth header or cookie. You'll have to look at the site data going back and forth to determine.
Michael Taylor
http://www.michaeltaylorp3.net