Background: The original B/s design in a JSP interface to achieve a number of complex workflow ...
The invocation of the mobile interface ensures that the normal webproject of the workflow specifically provides a scenario for the virtual browser
Principle: Through this scheme, the virtual browser runs the JSP script in the background to achieve our requirements
Requirements:
1, the session is maintained. Otherwise it will always jump to the login interface
2, JSP script must be run, not just a simple request to initiate
First on
Private String Submithandler (httpservletrequest request,string msg,string sessionid,string pathjsp) throws IOException {//Virtual browser impersonation commit Jspurl URL = null;//Add browser version number remove Try catch WebClient client = new WebClient (browserversion.internet_ex PLORER_10); Client.getoptions (). Setjavascriptenabled (True); Client.getoptions (). setcssenabled (false); Client.getoptions (). Setthrowexceptiononfailingstatuscode (false); Client.getoptions (). Setthrowexceptiononscripterror (false); Client.getoptions (). Setredirectenabled (True); url = new URL ("http://localhost:" + request.getserverport () +pathjsp); System.out.println ("sfq--url====" + URL);//Jump Login View Cookie//htmlpage page = client.getpage (URL); WebRequest webRequest2 = new WebRequest (URL, httpmethod.post); list<namevaluepair> reqParam2 = new arraylist<namevaluepair> (); Reqparam2.add (New Namevaluepair (" Paramters ", msg)); System.out.println ("sfq--url====msg:" + msg); Webrequest2.setadditionalheader ("CookiE "," jsessionid= "+sessionid); Webrequest2.setrequestparameters (REQPARAM2);//client.getoptions (). Setthrowexceptiononscripterror (TRUE); HtmlPage Page2 = (htmlpage) client.getpage (WEBREQUEST2); System.out.println ("sfq--pageastext====" + page2.astext ()); WebResponse WebResponse = Page2.getwebresponse (); int status21 = Webresponse.getstatuscode (); System.out.println ("sfq--status21====" + STATUS21); if (status21==200) {return "1"; }else{return status21+ ""; }}
A few notes:
1, because the JSP in the project was originally only for the Web service. Conversion to mobile end of the interactive use of all kinds of error, just do not affect the process. I chose to ignore them.
Therefore, the try catch exception caught is removed. (Capturing is really in the way.)
。 )、
2, the session to keep the problem tangled for a long time, try the online method is probably the character of serious problems, not good (the following method really does not make ....)
)
private void Setcookies (WebClient webclient,string domain, map<string, string> cookies) {if (cookies! = null &&A mp Cookies.size () > 0) {webclient.getcookiemanager (). Setcookiesenabled (true);//enable//CookiesFor (entry<string , string> C:cookies.entryset ()) {Cookie cookie = new Cookie (domain, C.getkey (), C.getvalue ()); Webclient.getcookieman Ager (). Addcookie (cookie);}}}
3, under the guidance of our leadership to use the method of Requet success
Webrequest2.setadditionalheader ("Cookie", "jsessionid=" +sessionid);
Of course, you can also do a one-step simulation before requesting the JSP you want to use. Thus obtaining a cookie ... Which, of course, affects operational efficiency.
4, not easy in the local test passed, moved to the external network server after the direct connection timeout, and finally through the replacement URL IP address for locslhost, problem resolution
Add:
Localhoost, 127.0.0.1 and local IP differences (Network Foundation is too poor ...)
。。
。。 )
No1:
LocalHost is also called local, and the correct explanation is: the native server
The correct explanation for 127.0.0.1 in Windows and other systems is: Native address (native server)
Their resolution through the host file of this machine, Windows itself proactively resolves localhost to 127.0.0.1
NO2:
Localhot (local) is not transmitted via the NIC. This is important, and it is not limited by network firewalls and Nic-related.
127.0.0.1 is transmitted through the network card, relying on the network card. and is subject to network firewalls and Nic-related restrictions.
The native IP is also transmitted through the network card. Depends on the NIC and is subject to network firewall and Nic-related restrictions.
However, the difference between native IP and 127.0.0.1 is:
127.0.0.1 can only be interviewed by this machine.
Native IP can also be interviewed via external access via this computer
The general Setup program when the local service with localhost is the best, localhost will not be resolved to IP, and will not occupy the network card, networking resources.
Sometimes it can be used with localhost, but it's not the case with 127.0.0.1.
Guess when localhost visits, the system with the local current user's permission to visit, while using IP. is equal to this machine is through the network again to visit the machine. User's permissions to the network.
Summary below:
1, access to the local server, whether in terms of efficiency or usefulness of direct use of localhost is definitely the top choice, not to be disturbed by other problems
2, in the company's corporate project ignorance of the choice of the local IP, the lowest efficiency choice. Visit to go through the outside network, firewall, no no no no no no, find yourself ...
A lot of other references;
1, Hmlunit http://shenbai.iteye.com/blog/1985844
2, http://blog.csdn.net/lu8000/article/details/10693091
3, http://shihlei.iteye.com/blog/2067707
4, Localho related
Interactive programming of Android and webserver data---3 web crawler implements JSP background execution of Virtual browser