I have a page www.senderdomain.com/sender.aspx, from which i need to write a string to another page in other domain www.receiverdomain.com/receiver.aspx
In sender.aspx开发者_JAVA百科 i have written
Response.Write("Hello");
Response.Redirect(Request.UrlReferrer.ToString());
It gets redirected to respective receiver.aspx page, but I am not sure how to get the text "Hello" in receiver.aspx page. Can any pl help on this?
It seems you have a value on Sender.aspx that you need to display in receiver.aspx. This is how you can do it.
//On Page_Load of sender.aspx
Session["fromSender"] = "Hello";
Respone.Redirect("receiver.aspx");
Response.End();
//On Page_Load of receiver.aspx
if(!string.IsNullOrEmpty(Session["fromSender"].ToString()))
Response.Write(Session["fromSender"].ToString());
EDIT
In case of change in domain, immediate easy way is to pass the value in query-string.
//On Page_Load of sender.aspx
Response.Redirect("http://www.receiverdomain.com/receiver.aspx?fromSender=Hello");
Response.End();
//On Page_Load of receiver.aspx
if(!string.IsNullOrEmpty(Request.QueryString["fromSender"].ToString()))
Response.Write(Request.QueryString["fromSender"].ToString());
You may observe that the code pattern remains the same and container that is used to transfer the value changes from Session to QueryString.
EDIT2
If security is a concern with you in this case and you don't wish to expose the value ["Hello"
], then here comes another way that can help you. In this solution we will first redirect the page to receiver and then from receiver it shall ask for the value to sender. So first we'll write the code for receiver.
//On Page_Load of receiver.aspx
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
//Remember to use System.Net namespace
HttpWebRequest requestToSender = (HttpWebRequest)WebRequest.Create("http://www.senderdomain.com/sender.aspx?cmd=getvalue");
HttpWebResponse responseFromSender = (HttpWebResponse)requestToSender.GetResponse();
string fromSender = string.Empty;
//Remember to use System.IO namespace
using (StreamReader responseReader = new StreamReader(responseFromSender.GetResponseStream()))
{
fromSender = responseReader.ReadToEnd();
}
Response.Write(fromSender);
Response.End();
}
}
And in the sender.aspx
//On Page_Load of sender.aspx
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
if (!string.IsNullOrEmpty(Request.QueryString["cmd"].ToString()))
{
string cmd = Request.QueryString["cmd"].ToString();
if (cmd.Equals("getvalue", StringComparison.OrdinalIgnoreCase))
{
Response.Clear();
Response.Write("Hello");
Response.End();
}
}
Response.Redirect("http://www.receiverdomain.com/receiver.aspx");
Response.End();
}
}
You need to pass the value in the url or post it in a cross page postback.
For secure cross domain communication, take a look at SAML (Security Assertion Markup Language). It is a standard way of passing information securely across domain boundaries. It is most often used in Single Sign On scenarios, but it can be used to pass data securely. Are you using certificates? What type of encryption are you using?
Another option would be to save state to a database or filesystem that is accessible to both domains.
pass data in query string because can not do like this
for example
Response.Redirect(Request.UrlReferrer.ToString() + "?mytext=hello");
And in receiver page access querystring data, will resolve your issue.
use private algorithm like
string message = "hello";
add 1 to each char so that hello become ifmmp
and on receiver side -1 from each char so it will be hello
The Response.Redirect
method will scrap everything that you have written to the page, and replace it with a redirection page, so you can't send any content along with the redirect.
The only option to send data along in a redirect (that works between differnt domains and different servers) is to put it in the URL itself. Example:
string message = "Hello";
Response.Redirect(Request.UrlReferrer.ToString() + "?msg=" + Server.UrlEncode(message));
Another option is to output a page containing a form that is automatically posted to the destination:
string message = "Hello";
Response.Write(
"<html>" +
"<head><title>Redirect</title></head>" +
"<body onload=\"document.forms[0].submit();\">" +
"<form action=\"" + Server.HtmlEncode(Request.UrlReferrer.ToString()) + "\" method=\"post\">" +
"<input type=\"hidden\" name=\"msg\" value=\"" + Server.HtmlEncode(message) + "\">" +
"</form>" +
"</body>" +
"</html>"
);
Response.End();
You can use Request.Form["msg"]
on the recieving page to get the value.
Don't use the built-in URL encode, if you want to avoid all sorts of problems later.
String UrlEncode(String value)
{
StringBuilder result = new StringBuilder();
foreach (char symbol in value)
{
if ("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_.~".IndexOf(symbol) != -1) result.Append(symbol);
else result.Append("%u" + String.Format("{0:X4}", (int)symbol));
}
return result.ToString();
}
The above supports unicode, and pretty much everything.
精彩评论