2016-10-12 3 views
1

다음 헤더를 나머지 게시물에 추가하려고합니다 ... 일반 Java에서 작동하지만 저지 클라이언트 라이브러리를 사용하여 다시 쓰려고합니다. 내가 저지와 함께 게시물을 만드는 API 문서에 나와 있지 않은 오류 코드를 얻을 수 있도록 그냥 누락 된 헤더와 같은 작은 문제가 있어야한다는 것을 알고 ... 어떤 생각을 내가 바닥 함수에서 잘못하고있는거야?Java Jersey-client 헤더 문제

private void SetDefaultHeaders(HttpURLConnection conn) { 
     setRequestProperty(conn, "Accept", "*"); 
     setRequestProperty(conn, "Content-Type", "application/x-www-form-urlencoded"); 
} 

저지 코드 :

public void logIn(String email, String password) { 
     if (email != "" && email != null && password != "" && password != null) { 
      try { 
       StringBuilder sb = new StringBuilder(); 
       sb.append(Settings.WIFIPLUG_URL); 
       sb.append("/user_login"); 

MultivaluedMap<String, String> body = new MultivaluedMapImpl(); 
       body.add("username=", email); 
       body.add("password=", password);  

       System.out.println("login url: " + sb.toString()); 

       WebResource webResource = Client.create(new DefaultClientConfig()).resource(sb.toString()); 

       WebResource.Builder builder = webResource.accept("*"); 
       builder.type("application/x-www-form-urlencoded"); 

       ClientResponse response = builder.post(ClientResponse.class, body); 

       if (response.getStatus() != 200) { 
        throw new RuntimeException("failed: http error code " + response.getStatus()); 
       } 
       System.out.println("Response from server: " + response.getEntity(String.class)); 

      } catch (Exception e) { 
       e.printStackTrace(); 
      } 
     } 
    } 

전체 바닐라 자바 ​​로그인 기능 :

public String postUserLogin(String username, String password) { 
     String result = ""; 
     // URL for API to login 
     String url = "https://wifiplugapi.co.uk:3081/zcloud/api/user_login"; 
     String requestParams = "username=" + username + "&password=" + password; 

     try { 
      URL obj = new URL(url); 
      System.out.println("login url: " + obj); 

      // Opens the connection 
      HttpsURLConnection con = (HttpsURLConnection) obj.openConnection(); 

      // Send POST request 
      con.setDoOutput(true); 
      con.setDoInput(true); 

      // Request Headers 
      con.setRequestMethod("POST"); 

      // Sets all the headers 
      SetDefaultHeaders(con); 

      OutputStreamWriter wr = new OutputStreamWriter(con.getOutputStream(), "UTF-8"); 
      wr.write(requestParams);// adds values to the request 
      wr.flush(); 
      wr.close(); 

      // Handles the response 
      StringBuilder sb = new StringBuilder(); 
      int responseCode = con.getResponseCode(); 
      if (responseCode == 200) { 
       // if the request was successful OK = 200 
       BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream(), "UTF-8")); 
       String line = null; 
       while ((line = br.readLine()) != null) { 
        sb.append(line + "\n"); 
       } 
       br.close(); 
       // Returns Token 
      } else { 
       // If the request was bad, reason will be printed 
       result = "Error, login request failed"; 
       throw new RuntimeException("Failed : HTTP error code : " + con.getResponseCode()); 
      } 

      result = sb.toString(); 

      // JSON Parser 
      JsonParser parser = new JsonParser(); 

      JsonObject resultObj = parser.parse(result).getAsJsonObject(); 
      con.disconnect(); 

      if (resultObj.get("token") != null) { 
       result = (resultObj.get("token")).toString(); 
       System.out.println("JSONObject Result (token): " + result); 
      } else { 
       System.out.println("result = " + result); 
      } 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 

     // returns token value in string ie. fdg573gb3789gv923378gy83g3 
     result = result.replaceAll("\"", ""); 
     return result; 
    } 
+1

무엇이 오류 메시지가 무엇입니까? 여기에 게시 할 수 있습니까? – Nurjan

+0

@Nurzhan 서버로부터의 응답 : { "errorCode": "21327", "info": "Invalid Request"} – user2094257

+0

'body.add'를 할 때 키에'='를 써서는 안됩니다. 그것은 당신을 위해 추가 될 것입니다 –

답변

1

당신은 =에서이 없어야

일반 자바 작품 헤더 함수를 추가 body.add 할 때 열쇠. 그것은 당신을 위해 추가 될 것입니다

MultivaluedMap<String, String> body = new MultivaluedMapImpl(); 
body.add("username=", email);  // remove the = 
body.add("password=", password); // remove the =