Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/317.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java HttpClientFormSubmit以获取OAUTH访问令牌_Java_Apache Commons_Apache Httpclient 4.x_Apache Commons Httpclient - Fatal编程技术网

Java HttpClientFormSubmit以获取OAUTH访问令牌

Java HttpClientFormSubmit以获取OAUTH访问令牌,java,apache-commons,apache-httpclient-4.x,apache-commons-httpclient,Java,Apache Commons,Apache Httpclient 4.x,Apache Commons Httpclient,我正在为ClientFormSubmit使用httpcomponents-client-4.2.5-bin。我使用Oauth登录到facebook。我的Oauth登录有以下步骤 首次使用登录facebook 提供它重定向到本地主机的登录详细信息,并在url中包含代码参数 我需要得到那个代码值 代码是 try { DefaultHttpClient httpclient = new DefaultHttpClient(); HttpGet httpget =

我正在为ClientFormSubmit使用httpcomponents-client-4.2.5-bin。我使用Oauth登录到facebook。我的Oauth登录有以下步骤

  • 首次使用登录facebook

  • 提供它重定向到本地主机的登录详细信息,并在url中包含代码参数

  • 我需要得到那个代码值

    代码是

     try {
            DefaultHttpClient httpclient = new DefaultHttpClient();
    
            HttpGet httpget = new HttpGet("https://www.facebook.com/dialog/oauth?client_id=358300034293206&redirect_uri=http://localhost:8080/");
    
            HttpResponse response = httpclient.execute(httpget);
            HttpEntity entity = response.getEntity();
    
            System.out.println("Login form get: " + response.getStatusLine());
            if (entity != null) {
                entity.consumeContent();
            }
            System.out.println("Initial set of cookies:");
            List<Cookie> cookies = httpclient.getCookieStore().getCookies();
            if (cookies.isEmpty()) {
                System.out.println("None");
            } else {
                for (int i = 0; i < cookies.size(); i++) {
                    System.out.println("- " + cookies.get(i).toString());
                }
            }
    
            HttpPost httpost = new HttpPost("https://www.facebook.com/dialog/oauth?client_id=358300034293206&redirect_uri=http://localhost:8080/");
    
            List <NameValuePair> nvps = new ArrayList <NameValuePair>();
            nvps.add(new BasicNameValuePair("email", "*****"));
            nvps.add(new BasicNameValuePair("pass", "*****"));
    
            httpost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
    
            response = httpclient.execute(httpost);
            entity = response.getEntity();
            System.out.println("Double check we've got right page " + EntityUtils.toString(entity));
    
            System.out.println("Login form get: " + response.getStatusLine());
            if (entity != null) {
                entity.consumeContent();
            }
    
            System.out.println("Post logon cookies:");
            cookies = httpclient.getCookieStore().getCookies();
            if (cookies.isEmpty()) {
                System.out.println("None");
            } else {
                for (int i = 0; i < cookies.size(); i++) {
                    System.out.println("- " + cookies.get(i).toString());
                }
            }
    
            httpclient.getConnectionManager().shutdown();
        }catch(Exception e)
            {
                System.out.println( "Something bad just happened." );
                System.out.println( e );
                e.printStackTrace();
            }
    
        }
    
    试试看{
    DefaultHttpClient httpclient=新的DefaultHttpClient();
    HttpGet HttpGet=新的HttpGet(“https://www.facebook.com/dialog/oauth?client_id=358300034293206&redirect_uri=http://localhost:8080/");
    HttpResponse response=httpclient.execute(httpget);
    HttpEntity=response.getEntity();
    System.out.println(“登录表单get:+response.getStatusLine());
    如果(实体!=null){
    entity.consumercontent();
    }
    System.out.println(“cookies的初始集:”);
    列表cookies=httpclient.getCookieStore().getCookies();
    if(cookies.isEmpty()){
    系统输出打印项次(“无”);
    }否则{
    对于(int i=0;i

    是否可以使用请求头获取重定向url?提前感谢。

    使用HttpClient 4.3 API

    CloseableHttpClient httpClient = HttpClients.createDefault();
    
    HttpClientContext context = HttpClientContext.create();
    HttpGet httpGet = new HttpGet("http://www.google.com/");
    CloseableHttpResponse httpResponse = httpClient.execute(httpGet, context);
    try {
        System.out.println("Response status: " + httpResponse.getStatusLine());
        System.out.println("Last request URI: " + context.getRequest().getRequestLine());
        URICollection redirectLocations = context.getRedirectLocations();
        if (redirectLocations != null) {
            System.out.println("All intermediate redirects: " + redirectLocations.getAll());
        }
        EntityUtils.consume(httpResponse.getEntity());
    } finally {
        httpResponse.close();
    }
    
    使用HttpClient 4.2 API

    DefaultHttpClient httpClient = new DefaultHttpClient();
    
    HttpContext context = new BasicHttpContext();
    HttpGet httpGet = new HttpGet("http://www.google.com/");
    try {
        HttpResponse httpResponse = httpClient.execute(httpGet, context);
        System.out.println("Response status: " + httpResponse.getStatusLine());
        HttpRequest req = (HttpRequest) context.getAttribute(
                ExecutionContext.HTTP_REQUEST);
        System.out.println("Last request URI: " + req.getRequestLine());
        RedirectLocations redirectLocations = (RedirectLocations) context.getAttribute(
                DefaultRedirectStrategy.REDIRECT_LOCATIONS);
        if (redirectLocations != null) {
            System.out.println("All intermediate redirects: " + redirectLocations.getAll());
        }
        EntityUtils.consume(httpResponse.getEntity());
    } finally {
        httpGet.releaseConnection();
    }