Note The code for this article is based on the Silverlight 2.0 official edition.
Silverlight 2.0 official release, there have been some changes in the HttpWebRequest, the previous code in the official version may not be able to run the specific
The main changes are:
1, the Request stream must be closed before calling Httpwebrequest.begingetresponse ();
Exception thrown by 2,httpwebrequest.endgetresponse (), before the official version, Httpwebrequest.beinggetresponse (), cross domain, cross protocol access to throw security exception, all other requests return 404 error, Now, the error of Httprequest.endgetresponse () is thrown as an exception, a security-related error is thrown SecurityException, and a unsuccessful request throws a WebException exception. Webexception.response is set to Httpstatuscode.notfound.
Because HttpWebRequest sends asynchronous requests, if you want to interact with the interface, it also involves thread synchronization, which reports "invalid cross-thread access" errors if you do not synchronize threads. The following is a practical example of submitting data to a Java Servlet address, returning the submitted data to the current interface. Of course, there's no problem submitting to the ASPX page, just add the following code to the Page_Load event-handling method:
protected void Page_Load(object sender, EventArgs e)
{
if (Request.RequestType.Equals("POST"))
{
Response.ClearContent();
Response.ClearHeaders();
Response.Write("您提交的数据是:" + Request.Params.Get ("data"));
Response.End();
return;
}
}
If you submit to the Java Servlet, the test code can write:
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.getWriter().print("您提交的数据是:" + request.getParameter("data"));
}
To perform cross-domain testing on this computer, you need to set up in the Hosts file, such as:
127.0.0.1 www.mengxianhui.com
127.0.0.1 www.xianhuimeng.com