gpt4 book ai didi

java - 获取重定向uri的位置

转载 作者:太空宇宙 更新时间:2023-11-04 12:51:42 25 4
gpt4 key购买 nike

这是我的网址:

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 请求)后,它将被重定向到此 URI:

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

如何获取重定向的URI并获取code值?

下面是代码片段:

String data = "email=p@f.com&password=Airtel@2017"; URL url = new 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");

                                     HttpURLConnection conn = (HttpURLConnection) url.openConnection();

con.setDoOutput(true);
OutputStreamWriter writer = new OutputStreamWriter(
con.getOutputStream());
writer.write(data);
InputStream is = con.getInputStream();
System.out.println( "redirected url: " + con.getURL() );


BufferedReader br = new BufferedReader(new InputStreamReader(
(conn.getInputStream())));
System.out.println( "redirected url2: " + con.getURL() );

String st = null;
String output;
System.out.println("Output from Server .... \n");
while ((output = br.readLine()) != null) {
System.out.println(output);

st += output;
}
Spdfast111 test = new Spdfast111();
Map<String, String> map=test.getFormParams(st);
URL url1 = new URL("https://login.something.com/v1/oauth/grant?value_one="+map.get("value_one")+"&value_two="+map.get("value_two")+"redirected_uri=https://login.something.com/v1/oauth/token");
System.out.println("==========================="+url1);
HttpURLConnection con1 = (HttpURLConnection) url1.openConnection();

System.out.println( "orignal url: " + con1.getURL() );
con1.connect();
System.out.println( "connected url: " + con1.getURL() );
InputStream is1 = connn.getInputStream();
System.out.println( "redirected url: " + con1.getURL() );
is1.close();

最佳答案

您可以看一下以下示例:

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";

关于java - 获取重定向uri的位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35771134/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com