Java 获取重定向uri的位置

Java 获取重定向uri的位置,java,url,uri,Java,Url,Uri,这就是我的URL的样子: 在java中发布后(http post请求),它将被重新定向到此URI: YrnYdnHdY 如何获取重定向URI并获取代码值 下面是代码片段: String data=“电子邮件=p@f.com&密码=Airtel@2017"; URL=新URL(“”) HttpURLConnection conn=(HttpURLConnection)url.openConnection(); con.设置输出(真); OutputStreamWriter writer=新的Ou

这就是我的URL的样子:

在java中发布后(http post请求),它将被重新定向到此URI:

YrnYdnHdY

如何获取重定向URI并获取代码值

下面是代码片段:

String data=“电子邮件=p@f.com&密码=Airtel@2017"; URL=新URL(“”)

HttpURLConnection conn=(HttpURLConnection)url.openConnection();
con.设置输出(真);
OutputStreamWriter writer=新的OutputStreamWriter(
con.getOutputStream());
writer.write(数据);
InputStream=con.getInputStream();
System.out.println(“重定向url:+con.getURL());
BufferedReader br=新的BufferedReader(新的InputStreamReader(
(conn.getInputStream());
System.out.println(“重定向的url2:+con.getURL());
字符串st=null;
字符串输出;
System.out.println(“从服务器输出…”\n);
而((output=br.readLine())!=null){
系统输出打印项次(输出);
st+=输出;
}
Spdfast111测试=新的Spdfast111();
Map Map=test.getFormParams(st);
URL url1=新URL(“https://login.something.com/v1/oauth/grant?value_one=“+map.get(“value\u one”)+”&value\u two=“+map.get(“value\u two”)+”重定向的=https://login.something.com/v1/oauth/token");
System.out.println(“=====================================”+url1);
HttpURLConnection con1=(HttpURLConnection)url1.openConnection();
System.out.println(“原始url:+con1.getURL());
con1.connect();
System.out.println(“连接的url:+con1.getURL());
InputStream为1=connn.getInputStream();
System.out.println(“重定向url:+con1.getURL());
is1.close();

您可以查看以下示例:


您可以查看以下示例:


这个例子怎么样

    // loop until no more redirections are 
    for (;;) {
        if (Thread.currentThread().isInterrupted()) {
            return null;
        }
        if(proxy != null) {
            ucn = downloadURL.openConnection(proxy);
        } else {
            ucn = downloadURL.openConnection();
        }
        HttpURLConnection hucn = doConfigureURLConnection(ucn);

        if(Thread.currentThread().isInterrupted())
            return null;

        ucn.connect();

        int rc = hucn.getResponseCode();
        boolean isRedirect = 
                rc == HttpURLConnection.HTTP_MOVED_TEMP ||
                rc == HttpURLConnection.HTTP_MOVED_PERM;
        if (!isRedirect) {
            break;
        }

        String addr = hucn.getHeaderField(HTTP_REDIRECT_LOCATION);
        URL newURL = new URL(addr);
        if (!downloadURL.getProtocol().equalsIgnoreCase(newURL.getProtocol())) {
            throw new ResourceRedirectException(newURL);
        }
        downloadURL = newURL;
    }

这里的完整代码:

这个例子怎么样

    // loop until no more redirections are 
    for (;;) {
        if (Thread.currentThread().isInterrupted()) {
            return null;
        }
        if(proxy != null) {
            ucn = downloadURL.openConnection(proxy);
        } else {
            ucn = downloadURL.openConnection();
        }
        HttpURLConnection hucn = doConfigureURLConnection(ucn);

        if(Thread.currentThread().isInterrupted())
            return null;

        ucn.connect();

        int rc = hucn.getResponseCode();
        boolean isRedirect = 
                rc == HttpURLConnection.HTTP_MOVED_TEMP ||
                rc == HttpURLConnection.HTTP_MOVED_PERM;
        if (!isRedirect) {
            break;
        }

        String addr = hucn.getHeaderField(HTTP_REDIRECT_LOCATION);
        URL newURL = new URL(addr);
        if (!downloadURL.getProtocol().equalsIgnoreCase(newURL.getProtocol())) {
            throw new ResourceRedirectException(newURL);
        }
        downloadURL = newURL;
    }

此处的完整代码:

请包含有关如何执行POST请求的代码。您可以从响应中读取URL。我只包含了代码。请看一看,请包括有关如何完成POST请求的代码。您可以从响应中读取URL。我只包含了代码。请看一看
    // loop until no more redirections are 
    for (;;) {
        if (Thread.currentThread().isInterrupted()) {
            return null;
        }
        if(proxy != null) {
            ucn = downloadURL.openConnection(proxy);
        } else {
            ucn = downloadURL.openConnection();
        }
        HttpURLConnection hucn = doConfigureURLConnection(ucn);

        if(Thread.currentThread().isInterrupted())
            return null;

        ucn.connect();

        int rc = hucn.getResponseCode();
        boolean isRedirect = 
                rc == HttpURLConnection.HTTP_MOVED_TEMP ||
                rc == HttpURLConnection.HTTP_MOVED_PERM;
        if (!isRedirect) {
            break;
        }

        String addr = hucn.getHeaderField(HTTP_REDIRECT_LOCATION);
        URL newURL = new URL(addr);
        if (!downloadURL.getProtocol().equalsIgnoreCase(newURL.getProtocol())) {
            throw new ResourceRedirectException(newURL);
        }
        downloadURL = newURL;
    }