2015-08-15 34 views
0

对不起,问你这个问题,但我有点新Java。跨越多个HttpURLConnection请求维护登录令牌

我有以下类别:

protected class LoginMethod{ 
     public LoginMethod() throws MalformedURLException, UnsupportedEncodingException, IOException{ 
     URL url = new URL("http://example.com/login"); 
     Map<String,Object> params = new LinkedHashMap<>(); 
     params.put("username", user); 
     params.put("password", password); 

     StringBuilder postData = new StringBuilder(); 
     for (Map.Entry<String,Object> param : params.entrySet()) { 
      if (postData.length() != 0) postData.append('&'); 
      postData.append(URLEncoder.encode(param.getKey(), "UTF-8")); 
      postData.append('='); 
      postData.append(URLEncoder.encode(String.valueOf(param.getValue()), "UTF-8")); 
     } 
     byte[] postDataBytes = postData.toString().getBytes("UTF-8"); 
     try{ 
      HttpURLConnection conn = (HttpURLConnection)url.openConnection(); 
      conn.setRequestMethod("POST"); 
      conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); 
      conn.setRequestProperty("Content-Length", String.valueOf(postDataBytes.length)); 
      conn.setDoOutput(true); 
      conn.getOutputStream().write(postDataBytes); 

      Reader in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8")); 
      for (int c = in.read(); c != -1; c = in.read()) 
       token += String.valueOf((char)c); 
      System.out.println(token); 
      new LoggedIn(); 
     }catch(IOException es){ 
      System.out.println(es); 
     } 
     } 

    } 

这将返回一个JSON字符串的一些标记。 而接下来类:

protected class DoStuff{ 
     public DoStuff() throws MalformedURLException, UnsupportedEncodingException, IOException, ParseException{ 
      JSONParser parser = new JSONParser(); 
      URL url = new URL("http://example.com/DoStuff"); 
      Map<String,Object> params = new LinkedHashMap<>(); 

      params.put("Do", "Stuff"); 
      StringBuilder postData = new StringBuilder(); 
      for (Map.Entry<String,Object> param : params.entrySet()) { 
       if (postData.length() != 0) postData.append('&'); 
       postData.append(URLEncoder.encode(param.getKey(), "UTF-8")); 
       postData.append('='); 
       postData.append(URLEncoder.encode(String.valueOf(param.getValue()), "UTF-8")); 
      } 
      byte[] postDataBytes = postData.toString().getBytes("UTF-8"); 
       try{ 
       HttpURLConnection conn = (HttpURLConnection)url.openConnection(); 
       conn.setRequestMethod("POST"); 
       conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); 
       conn.setRequestProperty("Content-Length", String.valueOf(postDataBytes.length)); 
       conn.setDoOutput(true); 
       conn.getOutputStream().write(postDataBytes); 

       Reader in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8")); 
       for (int c = in.read(); c != -1; c = in.read()) 
        stuff +=(char)c; 
       System.out.println(stuff); 
       new LogOut(); 
       }catch(IOException er){ 
        System.out.println(er); 
       } 

     } 

    } 

这将返回DoStuff事情。但是因为我不知道如何保持这些类之间的会话,所以我得到了401错误。 我的问题是如何保持这些类之间的会话或提示如何将它们放入支持会话的单个类中,记住它们由单独的按钮调用。 谢谢!

回答

0

正确的答案是: 在我们需要JSON解码接收的令牌,并添加第二类

conn.setRequestProperty("Authorization", token); 

This works。感谢StackOverflow的一切。

0

如果提到的“http://example.com”是一个web服务,您在响应中收到的令牌具有会话信息。所有你需要做的就是在调用dostuff方法时通过设置请求头来发回令牌。

登录响应:

{ "token":"ewryoiuasdjhie8417098412","expires":"jun 10 2015"} 

同时呼吁dostuff

conn.setRequestProperty("token", "ewryoiuasdjhie8417098412");