I am using webrequest and webresponse in my application. I want to click on a button same as webbrowser control and fill a textbox field. How can I do this from webrequest and webresponse?
You cant do that, they just return you the response stream..
I did a blog post on how to programmatically log into a website(I used Twitter in my example). This is basically what you want(filling in textboxes and submitting the information), if I understand your question correctly.
http://eclipsed4utoo.com/blog/log-website-programmatically/
Related
How can I fill inputs and click LogIn button by their IDs?
The question could have been asked, but I couldn't find answer.
You can't really fill the inputs or click buttons with a web request. What you can do is post data to the target of the form and the server side code should pick it up just like you filled in the inputs and clicked the button. If you're doing asp.net the target of the form will the the page itself so you could do something like this:
var request = new WebRequest(#"http:\\localhost\somePage.aspx");
request.Credentials= CredentialCache.DefaultCredentials;
request.Method = "Post";
var postString = "SomeTextBox=Foo&SomeOtherTextBox=Bar";
var byteData = Encoding.UTF8.GetBytes(postString);
Stream requestStream = request.GetRequestStream();
requestStream.Write(byteData, 0, byteData.Length);
requestStream.Close();
if you're using asp.net you should look into changing the ClientId mode of the controls to Predictable or you'll get SomeMasterPage_SomeContainer_SomeTextBox as your id (subject to change without notice as you add things into the control tree). For the button click you'll want to add __EVENTTARGET=SomeButtonName to your post string.
I am using a Hit on server and getting the content in Streamer.
I then use a string where I get the Html code of the website.
I have to use this in a WPF application.
Which control should I use where I can put a url which contains html code to display in my wpf and HOW??
string urlcode;
HttpWebRequest request = WebRequest.Create("http://google.com/") as HttpWebRequest;
HttpWebResponse response = request.GetResponse() as HttpWebResponse;
StreamReader streamr = new StreamReader(response.GetResponseStream());
urlcode = streamr.ReadToEnd();
Embed the WebBrowser control on the preview tab and pass the HTML into it using the NavigateToString or NavigateToStream methods.
Use the WebBrowser control and its NavigateToStream method:
XAML:
<Grid>
<WebBrowser Name="webBrower"/>
</Grid>
Code:
WebRequest request = WebRequest.Create("http://google.com");
webBrower.NavigateToStream(request.GetResponse().GetResponseStream());
This is a simplified example. You would at least have to close/dispose the response object when navigation has finished.
Use a NavigationWindow to contain the page. You can then call the Navigate(Uri) method to traverse from page to page.
MSDN entry
You can use web browser control ,pass string containg HTML control to its NavigateToString method
webB.NavigateToString(#"<html>HTML code go here </html>");
I have a button click event set up to retrieve a byte array object from my DB and it is then going to show the file in a new browser window. Right now I have this much:
Response.ContentType = "image/jpeg";
Response.AddHeader("content-length", fileBytes.Length.ToString());
Response.BinaryWrite(fileBytes);
where fileByes is my byte array. This is working perfectly, but I need to force this to open in a new window. I have tried adding the javascript to the response with response.write but that doesn't seem to work.
Writing your response is handled server side. Displaying your response is handled client side. You would have to tell your client to open a new window given the response from the server, e.g.
Get Image
Where getImage.aspx is the ASP.NET page responsible for serving the image/page.
You can't open a new window from server-side code. You'll need to call window.open() from JavaScript and pass in a URL to a page that returns the file.
You'd want to have your button click open the new browser window, which then makes the call to your code you have posted in your question. You're trying to do it sort of backwards.
Use a hyperlink with a URL to a blank .aspx,
pass a parameters in the URL as ?param=4¶m2 ... etc.
In the load event for the page place your response code there.
I am using the WebBrowser in WPF like this:
<WebBrowser
Name="SSSBrowser"
Margin="8,4,8,8"
Grid.Row="1"
dp:WebBrowserUtility.BindableSource="{Binding WebAddress}"/>
And in C# I am loading it simply, for now, like this:
private string _webAddress;
public string WebAddress
{
get { return "http://www.somewebsite.com/updates/message/message.htm"; }
set { _webAddress = value; }
}
What I would like to do is prevent it from displaying an error if they cannot reach the webpage, for whatever reason.
How do I keep tell if the website returned an error in code and disable the WebBrowser so that it doesn't give an error on screen to user?
Any help would be greatly appreciated!
Not sure via WPF, but if you use HttpWebRequest and HttpWebResponse to programatically try to fetch your url first, the response will give you the http StatusCode, i.e. 200, 404 etc. Might be useful if you want to check for a 200 first and disable the browser preemptively. Not exactly the answer to your question, but a possibility.
If I'm right Source property is not bindable because it's not a dependency property.
Check out this post:
databind the Source property of the WebBrowser in WPF
Herber
There doesn't seem to be an ErrorOpeningURL event for the WPF WebBrowser object, so, judging by previous experience, you could wire up to the Navigated event and check whether the URI is the IE error page (res://Error.html.. IIRC) or dig into the NavigationEventArgs for the WebResponse and check the headers.
To suppress the error, hide the WebBrowser component, perhaps by covering with a polite message - your user will want to know that the operation did not succeed, but doesn't have to see the navigation FAIL.
You could try using the NavigationService from System.Windows.Controls.Frame as indicated in this MSDN forum post. The WebResponse will always be null for the WebBrowser control in WPF (as described in the post).
C# 3.0
ASP.Net 2.0
IIS6
I have a regular [non-https] page. There is the standard one ASP.Net form on the page.
There are two "areas" of functionality on the page though. Login and "Get Quote". The login page needs to POST to HTTPS while the rest of the page [including the "other area"] form can't be HTTPS. In Java [JSP] and regular Html, we would just have two forms. One that posts to HTTPS and one that doesn't.
What is the way to handle this in ASP.Net [from one page]. I know that I could link to an HTTPS login.aspx page, but the business really would like the context together.
Any ideas?
Thanks,
The solution is to use asp.net to specify a "cross page postback", that is, you user the PostBackUrl property of any button control (LinkButton, Button, ImageButton etc.). This property allows you to post back to any page you like. Just set your PostBackUrl to the https version of your page and you're good to go (also make sure there are no url redirects active which force http on your page).
// ensure we send credentials over a secure connection
if (!HttpContext.Current.Request.IsSecureConnection)
{
string postbackUrl = HttpContext.Current.Request.Url.AbsoluteUri.Replace("http", "https");
LinkButton_Login.PostBackUrl = postbackUrl;
}
In your specific case you should set one of your buttons to post back to the https version, and the other to the http version (if you don't specify the PostBackUrl the default is to post back to the page itself as is).
You can have two forms on an aspx page. You just can't nest them.
On a page I built, I have one form that posts back to the page, and one that posts back to Google Checkout.
If you have to mix the contents of the page, put the https form at the bottom of the page (after the main form tag) and fill it with hidden fields. When the user clicks a button, use Javascript to assign values to the hidden fields and then post the https form.
You could do a manual post through code using the HttpWebRequest object for the login event and then write the returned response back to the user's stream.
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(webRequest.URL);
request.UserAgent = UserAgent;
request.ContentType = ContentType;
request.Method = "POST";
// Write your bytes of the login section here
Stream oStream = request.GetRequestStream();
oStream.Write(webRequest.BytesToWrite, 0, webRequest.BytesToWrite.Length);
oStream.Close();
// Send the request and get a response
HttpWebResponse resp = (HttpWebResponse)request.GetResponse();
// Read the response
StreamReader sr = new StreamReader(resp.GetResponseStream());
// return the response to the screen
string returnedValue = sr.ReadToEnd();
sr.Close();
resp.Close();
Response.Write(returnedValue);
I'm assuming from your context, that you are doing one thing or the other, not both at the same time.
Look at the PostbackURL of the button objects.
the login button can postback to "https://secure.login.com"
The other button can just postback to the page itself.
The problem here is that you'll still be posting back the login fields to the insecure page, which means they're not encrypted, and could be sniffed.
The quick and dirty workaround would be to have javascript clear the login fields before posting if the "Get Quote" button is pressed.
Are the HTTP and HTTPS pages on the same server / part of the same application?
If so you maybe able to use the Server.Transfer() method to keep the form intact but also have the HTTPS.
In ASP.Net 3.5 (maybe SP1--forget if it was in the base library or the SP) you can now set the "action" attribute. But that would make it post to HTTPS for both 'forms'.
If you want to have both forms on the same page, and determine which to post to at 'runtime', you'll have to do it with client-side code. Have client handlers on all objects that trigger post backs or hook into the _dopostback (or whatever it's called--to lazy to look it up) function, and have it check which button was pressed. If the non-secure page, then clear out any data in the login fields first. Then manually trigger the postback yourself to the correct page.
Couldn't you just do a Response.Redirect("https://.../Login.aspx"); in the Login button click event.