当我在浏览器中将以下字符串发送到控制单元时,我有 http://192.168.0.215/i_activate/aterm?40~00
并且继电器被激活。
我已经尝试了以下多种变体:
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://192.168.0.215/i_activate/aterm?40~00");
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
来自单元的 HTML 响应“FAIL”
我已经尝试以多种方式(NameValuePair 等)添加 40~00 并以不同的形式进行编码但没有成功,但我确信问题出在那里。
有什么想法吗?
最佳答案
问题是浏览器发送了一个GET
请求,其中参数在URL本身作为一个查询字符串,但是你发送一个POST
请求没有任何 body 数据。
使用 HttpGet
而不是 HttpPost
发送 GET
请求:
HttpClient httpclient = new DefaultHttpClient();
HttpGet httpget = new HttpGet("http://192.168.0.215/i_activate/aterm?40~00");
// Execute HTTP Get Request
HttpResponse response = httpclient.execute(httpget);
关于java - 在浏览器中发送 HTTP 命令有效,但在使用 httppost 发送时无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31302318/