I have a PowerShellScript that will call a webpage and the webpage is doing some code-behind work. The issue I am trying to resolve is that I'd like to make the webpage as a secured web page, but once I do that, I couldn't figure out a way to pass the username and password to the secured webpage from PowerShell Script. I am using the form authentication. I'd appreciate if anyone could provide a completed code sample so that I can just plug-in and do the magic. Thanks so much!
p.s. I've already tried the System.Net.NetworkCredential, but it seems it is for the basic authentication.
$url = "someurl"
$webclient = New-Object System.Net.WebClient
$webclient.DownloadString($url)
Need a little more information. You may be able to solve your issue with the following answer
How to post data to specific URL using WebClient in C#
But I'm curious, what page you are submitting your authentication form to? That page would have this information. If the form is returning data into PS variables. Then the above link shows how to post directly to a page
Related
I am doing a project of making a web-crawler,to get some information from a internal website. I found that the static pages could be catched by a few simple steps(setting cookies, and then use Cmdlet Invoke-RestMethod),but The data of dynamic pages are not be able to get this way. And also,I found The links on dynamic pages are not constantly, for example, it always constitutes of jsessionids or something,which always changing from time to time.
Can anyone please give me a hint to slove this problem? Can dynamic web pages be able to catched by crawler or not? Thanks...
maybe download strings, that should pull the HTML and you can pull your data from there even if its dynamic as its a snapshot in time.
or
$site = "cnn.com"
$sitedata = invoke-webrequest $site
$sitedata.rawcontent
I have a JavaEE Application. I am trying to implement OAuth.
But I am facing some strange issues:
As per the documentation to manually building the sign in web flow I have to provide a link like this https://www.facebook.com/dialog/oauth?client_id=1231298371123&display=popup&redirect_uri=http%3A%2F%2Flocalhost%3A8080%2Fc%2Fportal%2Fauth%2Ffacebook_login%3F&scope=email,public_profile,user_birthday&response_type=code%20token which will open the dialog. But the dialog is not opening.
And when the SSO is successful FB is redirecting to the url given above but the problem is FB is appending the query strings like state, code, etc with #. Something like: http://localhost:8080/c/portal/auth/facebook_login?#state=ASDASDASDASD&access_token=EAANXZAlBTi........ Because for this I cannot get the parameters in Java.
Do any one came across this kind of issue.
Please help.
Not sure if I understood the question right but if you want to read the string after # you can use the following code
URI uri = new URI("http://test.com/#something=some");
String fragment = uri.getFragment();
fragment will be everything after #
I'm wondering how to extract text from a password protected website using Matlab's "webread" function. I have the following code (part of which i got from here):
values=inputdlg({'Url','Username:','Password'});
options=weboptions('Username',values{2},'Password',values{3},'Timeout',Inf);
html=webread(values{1},options);
txt = regexprep(html,'<script.*?/script>','');
txt = regexprep(txt,'<style.*?/style>','');
txt = regexprep(txt,'<.*?>','');
But it gets stuck at the login window for every webpage I've tried. Help? Ideas? Thanks.
The weboptions username and password parameters are for basic HTTP authentication, which is different than logging into Stack Exchange, Gmail, etc though the username and password boxes on a web page.
Some sites provide other mechanisms that might allow you to log in (like OAuth), and the File Exchange has a smattering of clients.
Here is an OAuth interface for login into the flicker using Matlab, maybe it help you. but you have to be sure that your website support this authentification method (as "Matt Krause" mentioned in his answer).
Link to Tutorial
Lately I've been using cURL to post data back from a custom Magento controller to a custom page on the same website.
However, the way I do it somehow breaks Magento's log in data. So I've tried another way. Magento has cURL functionality built into it (Varien_Http_Adapter_Curl).
I've tried to Post through this, but so far it has been over my head and documentation on the web is fairly sparse. I need help with this. I've got a string with all the $_POST data ready to go. Please can someone tell me how to send it?
This:
$url="<URL>";
$curl = new Varien_Http_Adapter_Curl;
$curl->setConfig(array('timeout' => 15));
$curl->write(Zend_Http_Client::POST,$url, '1.1', array(), $poststring);
$result = $curl->read();
$curl->close();
...isn't sending data .
Edit:
I've tried the non-Magento cURL, but didn't know about session Data. I still have no Idea how to send session data, either.
Now, I've tried session variables, but the result is that I can set and extract data on one page, but when changing pages the data is lost. So, this can't be used currently between the controller and view.
better you can use the magento sessions
http://magento-rohan.blogspot.in/2012/03/magento-get-set-unset-session.html
here is this how to use it
You need to give us more information about what are you trying to achieve. Basically you need to tell us where are you sending POST request to? Perhaps another Magento instance or even same Magento website? Are you expecting user to have same session it is having now? Once you give us more info I will edit my answer. For now I will try to guess what is bothering you based on the input you gave.
When you are submitting POST request with curl from server side, that means that user is no longer interacting with the "page" you are trying to submit post request to.
If user is not interacting with it, that means it is not sending user session information.
Basically it looks like this:
Normally
Eric ->(Request with session info)-> Server (Oh it's you Eric, here is
the response just for you)
What are you trying to do
Eric ->(Request with session info)-> Server ->(Request without session
info)-> Server (This server doesn't know about Eric)
So to implement this correctly, if I am good at assuming what is your problem, just pass session information to the second server along with your request.
I will add more info if you tell me I am on the good track of understanding your problem.
---UPDATE---
You didn't explain your situation well. I am telling you this because your whole approach with the cURL may be bad decision from the start. For example, if you are trying to execute code in the same Magento codebase and that code is trapped inside some controller, perhaps you can refactor your code and encapsulate that logic inside some model and execute it directly.
But here is the example of passing session information over curl in plain php:
$strCookie = 'PHPSESSID=' . $_COOKIE['PHPSESSID'] . '; path=/';
curl_setopt( $curl, CURLOPT_COOKIE, $strCookie );
Cookie should perhaps be called "frontend" in Magento. And I checked Varien_Http_Adapter_Curl it doesn't have any method for seting CURLOPT_COOKIE option so I suggest you go with plain curl setup. You also have an option to extend adapter and add that option by your self. Just override "_applyConfig" method.
I am trying to upload an archive file (a Zip archive if that matters) to an existing API at an external site and while I can get the webclient to connect and get a token to the site the upload never seems to work, I continually get a 404 error. The problem may be on their side, but I seem to be the only one who has posted about it on their site, yet I wonder if I am using the UploadFile correctly, or if there is a way to capture the response from the command, which I'd like to know if only to improve my PowerShell coding.
What I am running is the following:
# Variables I want to use
$appArchive = "\\networkpath\Releases\CodeReleasePackage.zip"
$localArchive = "c:\temp\CodeReleasePackage.zip"
[string]$appUrl = "https://site.com/api/v1/apps/(id)/"
[string]$tokenUrl = "https://site.com/token"
[string]$uploadUrl = "https://site.com/api/v1/apps/(id)/?auth_token=$mytoken"
# $appCred = Get-Credential
$username = "email#address"
$password = "password"
# Create the web client
"Creating the client.`n"
$pgup = new-object System.Net.WebClient
$pgup.Credentials = New-Object net.NetworkCredential($username,$password)
"Going to the token page"
$pgToken = $pgup.UploadString($tokenUrl,"")
$mytoken = $pgToken -replace '{|}|"|:|token',''
"Now trying to upload the latest file with my token: $mytoken"
$pgup.UploadFile($uploadUrl,$appArchive)
I get the token which is something like {"token","hkjuihgkjhiuhhlkhuhk"} and not really knowing if there is a better way to handle the resulting token I did the replace to get rid of everything but the token string. Adding it to the URL is supposed to work but I just get 404 errors accorinding to Fiddler.
So I'd like to be able to capture the response from the site when I run $pgup.UploadFile() but when I do it seems like nothing is coming back, is there a particular way to capture that? I couldn't find a clear example and when I do something like $response = $pgup.UploadFile() I have nothing in $response. Anything that is useful for diagnosing webclient errors would be great, I'd like to learn more about this since it seems pretty useful
Im not sure if this is what your looking for, however here is a bit of code that I use to upload a zip file to a server.
$File = "yourfile.zip" #File to be uploaded
$ftp = "ftp://yourserver/directory #Location for the zip file to be uploaded
$webclient = New-Object System.Net.WebClient
$uri = New-Object System.Uri($ftp)
$webclient.Credentials = Get-Credential #Will prompt for user info. You can use your above way to gather credentials as well
$webclient.UploadFile($uri,$File)
As you can see its rather short and simple, and almost identical to your code but does not handle tokens.
It looks like I cannot access the site with PowerShell as the driver, for some reason I can never get the upload to work but I can read the pages on the site and actually get responses for my account. It may be their API doesn't like the way PowerShell is running the connection, although I have never been able to confirm anything from PhoneGap since they have not yet responded to my issues on this yet.
Edited: 6/14/12
The whole reason this was failing was due to the Apache implementation on their side not accepting the HTTP 1.1 request the way WebClient sends it. If I can get back to HTTP 1.0 then they say I should be able to complete this. Or I can wait for the OAUTH 2.0 implementation and just go from there.