2016-07-15 3 views

답변

3

내가이 샘플 코드는 생각 유

이 totlly

public static String invokePostMethod() throws AuthenticationException, ClientHandlerException, IOException { 

    Client client = Client.create(); 
    WebResource webResource = client.resource("http://localhost:8080/rest/api/latest/issue");     

    String data = "{"fields":{"project":{"key":"DEMO"},"summary":"REST Test","issuetype":{"name":"Bug"}}}"; 

    String auth = new String(Base64.encode(Uname + ":" + Password)); 
    ClientResponse response = webResource.header("Authorization", "Basic " + auth).type("application/json").accept("application/json").post(ClientResponse.class, data); 
    int statusCode = response.getStatus(); 

    if (statusCode == 401) { 
     throw new AuthenticationException("Invalid Username or Password"); 
    } else if (statusCode == 403) { 
     throw new AuthenticationException("Forbidden"); 
    } else if (statusCode == 200 || statusCode == 201) { 
     System.out.println("Ticket Create succesfully"); 
    } else { 
     System.out.print("Http Error : " + statusCode); 
    } 
    // ******************************Getting Responce body********************************************* 
    BufferedReader inputStream = new BufferedReader(new InputStreamReader(response.getEntityInputStream())); 
    String line = null; 
    while ((line = inputStream.readLine()) != null) { 
     System.out.println(line); 

    } 
    return response.getEntity(String.class); 
} 
후반 응답 FOT 주석과 유감을위한
+0

안녕하세요 덕분에 당신이 말해 줄 수 나를 위해 노력하는 데 도움이 으로 라이브러리를 사용하고 있습니까? –

+0

사용이 받는다는 불일치 평민 - HttpClient를 평민 - HttpClient를 3.1 ' –

+0

이봐, 난 당신이 더 빨리 내가 ​​멀리 당신이 날대로 작동하는 것 같다 준 코드이었다 대답 havent 한 미안 해요 나는 올바른 응답을하지만 난이 얻을 로그인하지 메신저 : 대한 <입력 유형 = "숨겨진"제목 = "ajaxUnauthorised"값 = ".. 당신은이 작업 로그인을 수행 할 수있는 권한이 없습니다"> 덕분에 이득을 모든 이 코드는 작동하지 않습니다 –

관련 문제