0

我在从代理后面连接到 gtalk/facebook 服务器时遇到问题。在 doPost 下的 loginservlet 中,我在与服务器建立连接之前指定了代理设置。代码如下:

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 response)
 */
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");
}

}

当我运行应用程序时,它仍然给我 500 状态错误。以下是堆栈跟踪

 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)

我通过传递代理检查了是否还有其他需要添加的内容,但我无法理解我哪里出错了?

谢谢:)

4

1 回答 1

2

XMPP != HTTP 因此没有理由期望 XMPP 客户端会使用 HTTP 代理。但是,Smack 似乎添加了HTTP 代理支持(除了之前的 SOCKS4 和 SOCKS5 代理支持)。ConnectionConfiguration可以接受ProxyInfo参数

据我从JavaDocs可以看出,这是您需要做的:

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();

另外,对于以后的问题,请注意

  1. 尽管您的问题被标记为“Smack”,但您没有发布 Smack 特定代码。显示使用的行URLConnection毫无意义,因为那不是您的连接失败的地方。
  2. 您的问题标题表示 Google Talk,但其他所有内容均指 Facebook。这是令人困惑和不明确的问题或代码,这些问题或代码不是您实际使用的,很可能会导致您和其他成员感到沮丧。您应该尽可能发布SSCCE
于 2011-04-12T16:21:19.303 回答