Relatively new to C#, but making good progress.
I'm currently trying to test a System.Net.WebRequest method. Using the useful http testing kit at https://httpbin.org/ - I am trying to pass network credentials to https://httpbin.org/basic-auth/user/passwd and to retrieve a successful connection. (Currently getting 401's)
The username is user, and the password is passwrd.
I have a simple form, and button which starts the request. However, as stated, its not working, and i'm getting a 401 error.
Here is what I have so far:
private void button1_Click(object sender, EventArgs e)
{
NetworkCredential myCred = new NetworkCredential(
"user", "passwrd", "https://httpbin.org");
// Create a request for the URL.
WebRequest request = WebRequest.Create(
"https://httpbin.org/basic-auth/user/passwd");
// If required by the server, set the credentials.
request.Credentials = myCred;
// Get the response.
WebResponse response = request.GetResponse();
// Display the status.
Console.WriteLine(((HttpWebResponse)response).StatusDescription);
// Get the stream containing content returned by the server.
Stream 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 and the response.
reader.Close();
response.Close();
}
The problem is with your credentials, you are assuming that domain is the HTTP domain, but that's only useful for something like Active Directory domains. Just remove that parameter (and fix the password) and it will work:
NetworkCredential myCred = new NetworkCredential("user", "passwd");
Related
When trying to call Google location to address Service, it gives me this message:
No connection could be made because the target machine actively
refused it 173.194.66.95:80
I use ASP.net 4 with C#.
This is my code:
protected void btn_FindAddress_Click(object sender, EventArgs e)
{
url = "http://maps.googleapis.com/maps/api/geocode/xml?latlng=";
query = LatY.Text + "," + LongX.Text + "&sensor=true";
Address.Text = url;
Uri uri = new Uri (url + query);
// Create a request for the URL.
WebRequest request = WebRequest.Create(uri);
//// If required by the server, set the credentials.
request.Credentials = CredentialCache.DefaultCredentials;
// Get the response.
WebResponse response = request.GetResponse();
// Display the status.
Status.Text = (((HttpWebResponse)response).StatusDescription);
// Get the stream containing content returned by the server.
Stream 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.
Address.Text = responseFromServer;
// Clean up the streams and the response.
reader.Close();
response.Close();
}
Most commonly, the outbound connection can be established (the remote server could be reached and a response was sent in a timely fashion) but the port you're trying to use is being blocked (a firewall or a proxy are the two most likely options I can think of).
It also might derive from the fact that there is no server on the other side of the connection: this may happen for example if you NAT port 80 to a system which doesn't have a web server. Being that you're invoking Google's url, I doubt it's the case, but I thought it worth of pointing out.
At the moment I am trying to HTTP response request in my Sharepoint WarmUp script, however for this, I will have to use this code
using (System.IO.StreamReader file = new System.IO.StreamReader(filePath))
while ((line = file.ReadLine()) != null)
{
try
{
WebRequest request = WebRequest.Create(line);
request.Proxy = null;
if ((userName == null) || (userName == ""))
{
request.Credentials = CredentialCache.DefaultCredentials;
}
else
{
CredentialCache myCache = new CredentialCache();
myCache.Add(new Uri(line), "NTLM", new NetworkCredential(userName, password, DomainName));
request.Credentials = myCache;
}
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
using (Stream dataStream = response.GetResponseStream())
using (StreamReader reader = new StreamReader(dataStream))
{
string responseFromServer = reader.ReadToEnd();
reader.Close();
dataStream.Close();
response.Close();
}
Thread.Sleep(2000);
}
I am not suppose to use any User Account to get response, however I can use Default Credentials that only works when am logged in at server, otherwise they wont gonna work i guess.
Is there any other way of doing it, can't use powershell as its MOSS not sharepoint 2010
cheers
You CAN use Powershell for the warm-up script. You can call stsadm command, make HTTP requests and even use the server object model once you do the proper imports.
This post uses powershell to retrieve available sites using stsadm. Once the script retrieves this list, it instantiates a WebClient object and hits each site URL.
If you only want to hit the sites in a list, you can simply use a WebClient object to do this.
Alternatively, if you have a working Search Service within your SSP, you can configure a scheduled crawl. Not only you'll benefit from up to date crawl results but your site will be warm-up natively without the need of an additional job doing it "just for warming up".
I am relatively new to working with web pages in C#. What I am trying to do is log into a particular website ( https://www15.swalife.com/PortalWeb/portal/cwaLogon.jsp ) and allowing the page to be redirected to the default page, and then navigating from there to (https://www15.swalife.com/csswa/ea/plt/accessELITT.do) and downloading the source code and output it to a string.
I have figured out how to download the source code via HTTPWebRequest and HTTPWebResponse but am having trouble on coding the logging in function. I assume I will have to do something with POST? I have looked at http://www.dreamincode.net/forums/topic/152297-c%23-log-in-to-website-programmatically/ also.
Thanks in advance!!
EDIT:
The code supplied by jimmyjambles works flawlessly, except it doesn't quite get me the source code of the page I wanted. The code suggests that the log-in process failed, but I believe with a little bit of tweaking I could get it to work...also to everyone having issues with:
ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(AcceptAllCertifications);
Try changing your "public string" and "public bool" functions to "public static string" and "public static bool" respectively :)
EDIT 2:
Response HTML:
"<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">\n<HTML>\n<HEAD>\n\n\n\n\n\n\n<META http-equiv=\"Content-Type\" content=\"text/html; charset=ISO-8859-1\">\n<META name=\"GENERATOR\" content=\"IBM WebSphere Studio\">\n<TITLE>endSession.jsp</TITLE>\n<LINK rel=\"stylesheet\" href=\"eipPortletStyles/swalife.css\" type=\"text/css\">\n\t<script type=\"text/javascript\" language=\"JavaScript\" \n\t\tsrc=\"eipCommonJavaScript/eipGeneralFunctions.js\"/> </script>\n\t\t\n<script type=\"text/javascript\">\n\n\tfunction refreshParent()\n\t{\n\t if(window.parent)\n\t {\n\t if(window.parent.name == 'appMainFrame')\n\t window.parent.location = \"/csswa/ea/plt/logout.do\";\n\t // alert('Your session has expired. Please login again. ');\n\t }\n\t}\n\n</script>\n</HEAD>\n<BODY onload=\"refreshParent();\">\n \n\t \t<div class=\"eipErrors\">\n \t\t\t<div class=\"legendLabel\">Message</div>\n \t\t\t\n \t\t\t <div class=\"errorsHeader formTitle\">You Have Exited Out of Crew Web Access.<br> \t\t\t \n \t\t\t </div>\n \t\t\t \n \t\t\t<div class=\"errorsHeader formTitle\"> Please Close this Window and <font size=\"+1\">Log Out of SWALife</font> to Complete the Log Out Process. </div>\n \t\t<div class=\"errorsText\">\n \t\t \n \t\t\t\t\n \t\t</div>\n \t\t\n \t\t\t\n \t\t\n \t\t<div class=\"errorsFooter\">You will need to log back in before continuing.</div> \t\n \t\t\n \t</div>\n \n</BODY>\n</HTML>\n"
In order to use HttpWebRequest to access a secondary URL after login you will need to keep in mind a few things.
Firstly as Casperah has mentioned you will need to inspect the login form and identify the "name" attribute of the controls used to receive the login data.
Once you have done this you will need to format a post string accordingly and provide it to the WebRequest.
The last consideration is that once you have logged in, you will need to store and maintain the cookie assigned to you from the server that keeps you logged in.
I have taken a WebRequest snippet from this msdn article and modified it to perform a second page request after the login.
string loginurl = "http://www.gmail.com";
string secondurl = "http://mail.google.com/prefs";
string username = "bob#gmail.com";
string password = "12345";
GetSecondaryLoginPage(loginurl, secondurl, username, password);
public string GetSecondaryLoginPage(string loginurl, string secondurl, string username, string password, string cookieName = null)
{
// Create a request using a URL that can receive a post.
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(loginurl);
// Set the Method property of the request to POST.
request.Method = "POST";
CookieContainer container = new CookieContainer();
if (cookieName != null)
container.Add(new Cookie(cookieName, username, "/", new Uri(loginurl).Host));
request.CookieContainer = container;
// Create POST data and convert it to a byte array. Modify this line accordingly
string postData = String.Format("username={0}&password={1}", username, password);
ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(AcceptAllCertifications);
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();
// 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();
using (StreamWriter outfile =
new StreamWriter("output.html"))
{
outfile.Write(responseFromServer.ToString());
}
// Clean up the streams.
reader.Close();
dataStream.Close();
response.Close();
request = (HttpWebRequest)WebRequest.Create(secondurl);
request.CookieContainer = container;
response = request.GetResponse();
// Get the stream containing content returned by the server.
dataStream = response.GetResponseStream();
// Open the stream using a StreamReader for easy access.
reader = new StreamReader(dataStream);
// Read the content.
responseFromServer = reader.ReadToEnd();
// Clean up the streams.
reader.Close();
dataStream.Close();
response.Close();
return responseFromServer;
}
public bool AcceptAllCertifications(object sender, System.Security.Cryptography.X509Certificates.X509Certificate certification, System.Security.Cryptography.X509Certificates.X509Chain chain, System.Net.Security.SslPolicyErrors sslPolicyErrors)
{
return true;
}
The only added lines are for the postData and cookies.
You will need to modify the line
string postData = String.Format("username={0}&password={1}", username, password);
based on your controls on the form, since you posted the site your trying to work with I can guess that you are probably looking for
string postData = String.Format("uid={0}&portalBase=cwa&password={1}", username, password);
Using a WebBrowser in a form is fairly easy. Start by navigating to the cwaLogon.jsp page and locate the input-controls and Invoke "click" on the submit button.
Either that or make the appropriate GET/POSTs using HTTPWebRequest and Response.
Using Fiddler2 to check what to post is a great start.
I know this question has been asked quite a lot of times which is how I have got to where I am at with the code below however I just can't get it to work on the particular website I am trying to access. At the site I am trying to access I need to retrieve certain values from the page however things like price and availability only come up after logging in so I am trying to submit my login information and then go to the product page to get the information I need using HTML Agility Pack.
At the moment it seems to attempt the login however the website is either not accepting it or the cookies are not present on the next page load to actually keep me logged in.
If someone could help me with this I would be very grateful as I am not a programmer but have been assigned this task as part of a software installation.
protected void Button5_Click(object sender, System.EventArgs e)
{
string LOGIN_URL = "http://www.videor.com/quicklogin/1/0/0/0/index.html";
string SECRET_PAGE_URL = "http://www.videor.com/item/47/32/0/703/index.html?scriptMode=&CUSTOMERNO=xxx&USERNAME=xxx&activeTabId=0";
// have a cookie container ready to receive the forms auth cookie
CookieContainer cookies = new CookieContainer();
// first, request the login form to get the viewstate value
HttpWebRequest webRequest = WebRequest.Create(LOGIN_URL) as HttpWebRequest;
webRequest.CookieContainer = cookies;
StreamReader responseReader = new StreamReader(
webRequest.GetResponse().GetResponseStream()
);
string responseData = responseReader.ReadToEnd();
responseReader.Close();
string postData = "CUSTOMERNO=xxxx&USERNAME=xxxxx&PASSWORD=xxxxx";
// now post to the login form
webRequest = WebRequest.Create(LOGIN_URL) as HttpWebRequest;
webRequest.Method = "POST";
webRequest.ContentType = "application/x-www-form-urlencoded";
webRequest.CookieContainer = cookies;
// write the form values into the request message
StreamWriter requestWriter = new StreamWriter(webRequest.GetRequestStream());
requestWriter.Write(postData);
requestWriter.Close();
// we don't need the contents of the response, just the cookie it issues
webRequest.GetResponse().Close();
// now we can send out cookie along with a request for the protected page
webRequest = WebRequest.Create(SECRET_PAGE_URL) as HttpWebRequest;
webRequest.CookieContainer = cookies;
responseReader = new StreamReader(webRequest.GetResponse().GetResponseStream());
// and read the response
responseData = responseReader.ReadToEnd();
responseReader.Close();
Response.Write(responseData);
}
This isn't a direct answer since I'm not sure what's wrong with your code (from a cursory glance it looks ok), but another approach is to use browser automation using Selenium . The following code will actually load the page using Chrome (you can swap out Firefox or IE) and is simpler to code against. It also won't break if they add javascript or something.
var driver = new ChromeDriver();
driver.Navigate().GoToUrl(LOGON_URL);
driver.FindElement(By.Id("UserName")).SendKeys("myuser");
driver.FindElement(By.Id("Password")).SendKeys("mypassword");
driver.FindElement(By.TagName("Form")).Submit();
driver.Navigate().GoToUrl(SECRET_PAGE_URL);
// And now the html can be found as driver.PageSource. You can also look for
// different elements and get their inner text and stuff as well.
private void button1_Click(object sender, EventArgs e)
{
string userName = textBox1.Text;
string password = textBox2.Text;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://www.youmint.com/LoginVerification.php?name="+userName+"&pass="+password+"&agreement=true&checkvalue=true");
request.Method = "GET";
request.KeepAlive = true;
request.Headers.Add("Keep-Alive: 300");
WebResponse response = request.GetResponse();
Stream dataStream = response.GetResponseStream();
StreamReader reader = new StreamReader(dataStream);
string responseFromServer = reader.ReadToEnd();
if (responseFromServer.Equals(""))
MessageBox.Show("Successfully logged in!!");
else if (responseFromServer.Equals("1"))
MessageBox.Show("Login failed!");
request = (HttpWebRequest)WebRequest.Create("http://www.youmint.com/FreeSms.html");
response = request.GetResponse();
dataStream = response.GetResponseStream();
reader = new StreamReader(dataStream);
responseFromServer = reader.ReadToEnd();
/*secret code :P */
reader.Close();
dataStream.Close();
response.Close();
}
So, that's my code... The first webrequest logs into the website. It works fine in the browser and returns 1 if the login is not correct. Then the second one is a normal webrequest to a webpage of the same website. But the login is already gone and the response I get is what I get if I'm not logged in! Can someone please tell me what I'm doing wrong? How do I keep it alive? Do I have to use an invisible webbroswer control or something like that?
This has nothing to do with "keep alive".
You need to preserve session cookie between requests. For that you first need to enable cookies for your login request (read HttpWebRequest docs - it is a bit unobvious). Then you need to pass that cookie with all the following reuests.
Also please make use of the using()
#liho1eye is correct. Here's some more info from the HttpWebRequest page:
For security reasons, cookies are
disabled by default. If you want to
use cookies, use the CookieContainer
property to enable cookies.
You'll need to reference the HttpWebResponse.Cookies property to get the initial session token cookie.
Edit:
Here's a quick and dirty sample of making a request to a page, and transferring response cookies to the next request. Didn't do much testing or validation (so beware!) - just to give you the idea of the approach.
//this only has login/password info, you may need other parameters to trigger the appropriate action:
const string Parameters = "Login1$username=pfadmin&Login1$Password=password";
System.Net.HttpWebRequest req = (HttpWebRequest)System.Net.WebRequest.Create("http://[WebApp]/Login.aspx");
req.Method = "GET";
req.CookieContainer = new CookieContainer();
System.Net.HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
//Create POST request and transfer session cookies from initial request
req = (HttpWebRequest)System.Net.WebRequest.Create("http://localhost/AdminWeb/Login.aspx");
req.CookieContainer = new CookieContainer();
foreach (Cookie c in resp.Cookies)
{
req.CookieContainer.Add(c);
}
req.ContentType = "application/x-www-form-urlencoded";
//...continue on with your form POST