I am having problems connecting to gtalk/facebook server from behind a proxy .In my loginservlet under doPost I specify the proxy settings before making a connection with the servers.The code is as follows:
package web;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.jivesoftware.smack.ConnectionConfiguration;
import org.jivesoftware.smack.SASLAuthentication;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.proxy.ProxyInfo;
import dao.MySASLDigestMD5Mechanism;
public class LoginFacebookServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public LoginFacebookServlet() {
super();
}
XMPPConnection connection;
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse resp开发者_如何学Goonse)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String userName = request.getParameter("usrnm_fb");
String password = request.getParameter("password_fb");
response.setContentType("text/html");
PrintWriter pw=response.getWriter();
SASLAuthentication.registerSASLMechanism("DIGEST-MD5", MySASLDigestMD5Mechanism.class);
//SASLAuthentication.supportSASLMechanism("DIGEST-MD5", 0);
ProxyInfo proxyInfo = new ProxyInfo(ProxyInfo.ProxyType.HTTP,"proxy.xxx.com" "talk.google.com", port, "username", "password");
ConnectionConfiguration config = new ConnectionConfiguration("chat.facebook.com",5222,proxyInfo);
connection = new XMPPConnection(config);
config.setSASLAuthenticationEnabled(true);
try {
connection.connect();
} catch (XMPPException e) {
e.printStackTrace();
}
try {
connection.login(userName, password);
} catch (XMPPException e) {
e.printStackTrace();
}
System.out.println(connection.isAuthenticated());
// System.out.println("Welcome!!you are now connected to facebook");
}
}
When I run the application it still gives me 500 status error.Following is the stack trace
XMPPError connecting to chat.facebook.com:5222.: remote-server-error(502) XMPPError connecting to chat.facebook.com:5222.
-- caused by: java.net.ConnectException: Connection timed out: connect
at org.jivesoftware.smack.XMPPConnection.connectUsingConfiguration(XMPPConnection.java:900)
at org.jivesoftware.smack.XMPPConnection.connect(XMPPConnection.java:1415)
at web.LoginFacebookServlet.doPost(LoginFacebookServlet.java:52)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
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.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:298)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:852)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
at java.lang.Thread.run(Thread.java:619)
Nested Exception:
java.net.ConnectException: Connection timed out: connect
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:333)
at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:195)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:182)
at java.net.Socket.connect(Socket.java:529)
at java.net.Socket.connect(Socket.java:478)
at org.jivesoftware.smack.proxy.DirectSocketFactory.createSocket(DirectSocketFactory.java:28)
at org.jivesoftware.smack.XMPPConnection.connectUsingConfiguration(XMPPConnection.java:888)
at org.jivesoftware.smack.XMPPConnection.connect(XMPPConnection.java:1415)
at web.LoginFacebookServlet.doPost(LoginFacebookServlet.java:52)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
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.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:298)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:852)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
at java.lang.Thread.run(Thread.java:619)
Apr 26, 2011 11:32:24 AM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet LoginFacebookServlet threw exception
java.lang.IllegalStateException: Not connected to server.
at org.jivesoftware.smack.XMPPConnection.login(XMPPConnection.java:382)
at org.jivesoftware.smack.XMPPConnection.login(XMPPConnection.java:349)
at web.LoginFacebookServlet.doPost(LoginFacebookServlet.java:57)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
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.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:298)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:852)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
at java.lang.Thread.run(Thread.java:619)
I checked to see if any thing more was there to add for by passing proxy but am unable to understand as to where am I going wrong?
Thanks:)
XMPP != HTTP so there's no reason to expect that an HTTP proxy will be used by an XMPP client. However, it seems like Smack has added HTTP proxy support (in addition to SOCKS4 and SOCKS5 proxy support they had earlier). The ConnectionConfiguration
can accept a ProxyInfo parameter.
As far as I can tell from the JavaDocs, this is what you need to do:
ProxyInfo proxyInfo = new ProxyInfo(ProxyInfo.ProxyType.HTTP, "proxy.xxx.com", 8080, "username", "password")
ConnectionConfiguration config = new ConnectionConfiguration("facebook.com", 5222, proxyInfo);
XMPPConnection conn = new XMPPConnection(config);
conn.connect();
Also, for future questions, please note
- Although your question is tagged 'Smack' you posted no Smack specific code. The lines showing use of the
URLConnection
are pointless since that's not where your connection is failing. - Your question title indicates Google Talk, however everything else refers to Facebook. This is confusing and unclear questions or code that is NOT what you're actually working with will most likely lead to frustration for you and other members. As far as possible, you should post an SSCCE.
精彩评论