获取重定向的uri的位置

时间:2016-03-03 11:37:27

标签: java url uri

这就是我的网址:

  

https://localhost.com.ua/oauth/authorize?client_id=SS&response_type=code&scope=N_FULL&access_type=offline&redirect_uri=http://localhost/

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

  

https://kuma.ruto/v1/0auth/grant?state=some&code= YrnYdnHdY

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

以下是代码段:

  

String data =“email = p@f.com& password = Airtel @ 2017”;                                            网址url =新网址(“https://login.something.com/v1/oauth/authorize?response_type=code&state=none&email=p@f.com&client_id=jkhsdhaskfhdash&password=Airtel@2017&redirect_uri=https://login.something.com&code=none”);

// Support for constraint-based layout (auto layout)
// If nonzero, this is used when determining -intrinsicContentSize for multiline labels
@property(nonatomic) CGFloat preferredMaxLayoutWidth NS_AVAILABLE_IOS(6_0);

2 个答案:

答案 0 :(得分:0)

您可以查看以下示例:

https://github.com/mconf/bbb-java/blob/master/src/main/java/org/mconf/bbb/api/JoinServiceBase.java#L151

        HttpClient client = new DefaultHttpClient();
        HttpGet method = new HttpGet(joinUrl);
        HttpContext context = new BasicHttpContext();
        HttpResponse httpResponse = client.execute(method, context);
        if (httpResponse.getStatusLine().getStatusCode() != HttpStatus.SC_OK)
            log.debug("HTTP GET {} return {}", joinUrl, httpResponse.getStatusLine().getStatusCode());
        HttpUriRequest currentReq = (HttpUriRequest) context.getAttribute( 
                ExecutionContext.HTTP_REQUEST);

        if (!currentReq.getURI().getPath().equals("/client/BigBlueButton.html")) {
            log.warn("It was redirected to {} instead of /client/BigBlueButton.html: the server was branded" +
                    " and the HTML name was changed, or it's an error. However, it will continue processing", currentReq.getURI().getPath());
        }

        HttpHost currentHost = (HttpHost) context.getAttribute( 
                ExecutionContext.HTTP_TARGET_HOST);
        String enterUrl = currentHost.toURI() + "/bigbluebutton/api/enter";

答案 1 :(得分:0)

这个例子怎么样?:

    // 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;
    }

此处的完整代码:http://code.openhub.net/file?fid=eLvMdX5b01HDEtWkU2TEPg13uu0&cid=xJ52R73llJU&s=Obtaining%20the%20location%20of%20the%20redirected%20uri&pp=0&fl=Java&ff=1&filterChecked=true&fp=1141&mp,=1&ml=1&me=1&md=1&projSelected=true#L0