2016-07-07 3 views
1

Я не уверен, что я на правильном пути. Поскольку я не получаю никаких данных с HTTP-запросом. Мне нужно было преобразовать этот код Java в force.com apex для меня, чтобы иметь возможность использовать веб-службу WTS.Force.com Apex часть счетчика или эквивалентный код для Java

public static HttpEntity getRequestEntity() 
{ 
    JSONArray jsonArr = new JSONArray(); 
    HttpEntity entity = null; 
    JSONObject jsonElement = new JSONObject(); 
    JSONObject jsonObj = new JSONObject(); 
    JSONObject jsonObj1 = new JSONObject(); 
    jsonElement.put("tenant",jsonObj); 
    jsonObj.put("companyKey",companyName); 

    String json = jsonElement.toJSONString(); 
    try { 
    entity = new StringEntity(json); 
    } catch (UnsupportedEncodingException e) { 
    e.printStackTrace(); 
    } 
    return entity; 
} 

private static void GetEnabledUsers(String responseContent) throws ParseException, ClientProtocolException, IOException { 
String url = "<endpoint>/services/UserService1.svc/GetEnabledUsers"; 

// Create an http client to perform the http communication 
CloseableHttpClient httpClient = HttpClients.createDefault(); 

// Http request to specific web service. Request will be given to client 
HttpPost request = new HttpPost(url); 

// Create credentials for the authentication 
CredentialsProvider provider = new BasicCredentialsProvider(); 

UsernamePasswordCredentials credential = new UsernamePasswordCredentials(companyName+'\\'+userName, password); 
provider.setCredentials(AuthScope.ANY, credential); 
HttpClientContext localContext = HttpClientContext.create(); 
localContext.setCredentialsProvider(provider); 

// Make the actual http request 
HttpPost httpRequest = new HttpPost(url); 

HttpEntity requestEntity = getRequestEntity(); 
if(requestEntity != null) { 
httpRequest.setHeader("Content-type", "application/json"); 
httpRequest.setEntity(requestEntity); 
} 

CloseableHttpResponse response = httpClient.execute(httpRequest, localContext); 
HttpEntity entity = response.getEntity(); 
String responseContent1 = EntityUtils.toString(entity); 
System.out.println(responseContent1); 
} 

Это то, что я до сих пор:

String userName= '<userName>'; 
String password= '<password>'; 
String companeyKey = '<companeyKey>'; 

JSONGenerator jsonGenerator = JSON.createGenerator(true); 
jsonGenerator.writeStartObject(); 
    jsonGenerator.writeFieldName('tenant'); 
    jsonGenerator.writeStartObject(); 
     jsonGenerator.writeStringField('companeyKey', '<companeyKey>'); 
    jsonGenerator.writeEndObject(); 
jsonGenerator.writeEndObject(); 

requestBody = jsonGenerator.getAsString(); 

Blob headerValue = Blob.valueOf(userName + ':' + password); 
HttpRequest requestEntity = new HttpRequest(); 
String authHeader = 'BASIC ' + EncodingUtil.base64Encode(headerValue); 

requestEntity.setMethod('POST'); 
requestEntity.setEndPoint('<endpoint>/services/UserService1.svc/GetEnabledUsers'); 
requestEntity.setHeader('Content-Type', 'application/json'); 
requestEntity.setHeader('Authorization', authHeader); 
requestEntity.setBody(requestBody); 

Http http = new Http(); 
HTTPResponse responseEntity = http.send(requestEntity); 

system.debug(responseEntity); 
+0

Похоже, вы на правильном пути. Что находится в журнале отладки? – superfell

+0

@superfell Я получаю ошибку 404 –

+0

, если хотите, System.HttpResponse [Status = Not Found, StatusCode = 404] –

ответ

1

О различии, кажется, вокруг, как имя пользователя строится для параметра AUTH, изменить

Blob headerValue = Blob.valueOf(userName + ':' + password);

в

Blob headerValue = Blob.valueOf(companyKey + '\\' + userName + ':' + password);