HttpClient PUT Method problem.for changing temperature


#1
    Hello.

I’d want to change thermostat temperature using HttpClient in Java.
I’ve tried to use httpPut a lot time in a variety of ways, but result code was always 307…(not 200).
When I use the urlConnection instead of HttpClient in java, I got 200 retrun code.
But I have to use Httpclient method in java to change temperatrue.
Here is my java code that uses Httpclient method.
Are there any problem or missing header/component in my code? plz give me advices…

        String result = "";
	InputStream iS = null;
	
	HttpClient httpClient = new DefaultHttpClient();
	HttpPut httpPut = new HttpPut("https://developer-api.nest.com/devices/thermostats/[MyDeviceId]/target_temperature_f?auth=[MyAccessToken]");
	httpPut.addHeader("Content-Type", "application/json");
	httpPut.addHeader("Accept", "application/json");
	//httpPut.addHeader("Authorization", "Bearer " + accessToken.get("accessToken"));
	httpPut.addHeader("Connection", "keep-alive");
	//httpClient.getParams().setBooleanParameter("http.protocol.expect-continue",false);
	String temperatures = "78";
	try {
		httpPut.setEntity(new StringEntity(temperatures));
		HttpResponse httpResponse = httpClient.execute(httpPut);		
        HttpEntity entity = httpResponse.getEntity();	        
        iS = entity.getContent();
        int statusCode = httpResponse.getStatusLine().getStatusCode();	
        
        System.out.println("StatusCode : " + statusCode);
        System.out.println("ResultData : " + httpResponse);
	} catch (UnsupportedEncodingException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();	
	} catch (ClientProtocolException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	} catch (IOException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}

#2

The 307 response is normal. It is a redirect request indicating that your request has to be redirected to one of the servers in the cluster. Your code needs to be able to handle these redirects. Depending on the library

https://developers.nest.com/documentation/cloud/how-to-handle-redirects

Also, here is a StackOverflow discussion on handling redirects for HttpClient.