我正在寻找这样的帮助:
我有网站A,我在其上设置Cookie。
在同一个浏览器中打开了网站B,我尝试使用代码从网站B连接到网站A:
String responseSession ="";
try {
URL url = new URL(urlSCS + "session" );
InputStream response = url.openStream();
BufferedReader reader = new BufferedReader(new
InputStreamReader(response, "UTF-8"));
for(String line; (line = reader.readLine()) != null;){
responseSession += line;
}
}catch(Exception e){
e.printStackTrace();
}
return responseSession;
从网站A获取Cookie。
问题是,当我用Cookie打开网站A时,Cookie显示正确,但是当我试图通过网站B获取Cookie时,我会收到错误:
SEVERE: Servlet.service() for servlet appServlet threw exception
java.lang.NullPointerException
at com.esb.scs.SessionController.session(SessionController.java:81)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:176)
at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:436)
at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:424)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:790)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:719)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:669)
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:574)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.springframework.orm.hibernate3.support.OpenSessionInViewFilter.doFilterInternal(OpenSessionInViewFilter.java:198)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:291)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:859)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:602)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
at java.lang.Thread.run(Thread.java:680)
答案 0 :(得分:3)
Cookie无法在此类网站上共享。
当您设置cookie时,它是您的浏览器和网站A之间的合同。当您的浏览器连接到网站B时,它无法访问网站A的cookie,因为cookie在您的浏览器上。您不能将此cookie传输到网站B,以便它可以在网站A上使用它。这是出于安全原因而无法解决的。
要将信息从网站A传输到网站B,您应该使用从网站A到网站B的表格POST操作,并对帖子中所需的数据进行编码。然后,网站B可以从POST操作中读取此信息,而不是尝试从网站A上的cookie中获取该信息。