Java Server重定向次数过多。需要帮助查找新的URL

时间:2014-12-17 22:54:01

标签: java httpurlconnection

我正在处理连接到okcupid.com的应用程序,并使用有效的用户名和密码登录。我在测试期间运行正常,但最近在okc服务器端发生了一些变化,因为我收到的错误表明服务器重定向次数过多(2)(参见下面的错误输出)。

直到最近,使用的代码(http://www.okcupid.com/locquery?func=query&query=" + zip)如果我转到此URL或使用下面发布的学习循环的代码,我仍然可以获得输出,但我的应用程序,使用帐户登录停止工作。我认为cookie发生了变化。大多数错误消息都会返回到位置ID。有人可以解释如何使用Chrome或Firefox在通过浏览器登录okc时查明此URL,以便我可以在代码中进行apprpopriate更改吗?提前谢谢。

  java.net.ProtocolException: Server redirected too many  times (20)
     at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1469)
     at okc.jurl.run(jurl.java:149)
     at java.lang.Thread.run(Thread.java:695)
 Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 1
    at okc.Utils.find(Utils.java:60)
    at okc.LocationAssistant.findLocid(LocationAssistant.java:125)
    at okc.AccountManager.findLocid(AccountManager.java:390)
    at okc.OkcView.getLocid(OkcView.java:3088)
    at okc.OkcView.login(OkcView.java:2924)
    at okc.OkcView.access$20(OkcView.java:2912)
    at okc.OkcView$15.actionPerformed(OkcView.java:1114)
    at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2028)
    at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2351)
    at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
    at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
    at java.awt.Component.processMouseEvent(Component.java:6414)
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3275)
    at java.awt.Component.processEvent(Component.java:6179)
    at java.awt.Container.processEvent(Container.java:2084)
    at java.awt.Component.dispatchEventImpl(Component.java:4776)
    at java.awt.Container.dispatchEventImpl(Container.java:2142)
    at java.awt.Component.dispatchEvent(Component.java:4604)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4618)
    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4279)
    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4209)
    at java.awt.Container.dispatchEventImpl(Container.java:2128)
    at java.awt.Window.dispatchEventImpl(Window.java:2492)
    at java.awt.Component.dispatchEvent(Component.java:4604)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:717)
    at java.awt.EventQueue.access$400(EventQueue.java:82)
    at java.awt.EventQueue$2.run(EventQueue.java:676)
    at java.awt.EventQueue$2.run(EventQueue.java:674)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:86)
    at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:97)
    at java.awt.EventQueue$3.run(EventQueue.java:690)
    at java.awt.EventQueue$3.run(EventQueue.java:688)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:86)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:687)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:296)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:211)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:201)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:196)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:188)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)

1 个答案:

答案 0 :(得分:0)

获取值的网址似乎缺少func个关键字。

例如,以下网址有效:

https://www.okcupid.com/locquery?func=query&query=14850

{"status" : 0, "ZipCode" : "14850", "query" : "14850", "locid" : 4333788, 
"results" : [{"locid" : 4333788, "text" : "Ithaca"}]}

以下没有:

https://www.okcupid.com/locquery?=query&query=14850

{"query" : ""}

主要检查代理设置是否有任何变化。以下程序使用适当的代理设置。

import java.net.*;
import java.io.*;

public class URLReader {
    public static void main(String[] args) throws Exception {

        //Following line needs to be enabled accordingly if you are using proxy
        //If you are enabling proxy, replace proxy.com with your proxy address    
        //System.setProperty("https.proxyHost", "proxy.com");
        //System.setProperty("https.proxyPort", "80");

        URL oracle = 
            new URL("https://www.okcupid.com/locquery?func=query&query=14850");
        BufferedReader in = new BufferedReader(
        new InputStreamReader(oracle.openStream()));

        String inputLine;
        while ((inputLine = in.readLine()) != null)
            System.out.println(inputLine);
        in.close();
    }
}