使用Java HttpComponent/HttpClinet 调用 WebAPI问题的解决
2015-05-13 14:04
651 查看
几个关键字:
WebAPI, Android, Apache HttpComponent/HttpClient
问题:无论怎么测试, WebAPI的FormBody value总是空.
最简单的代码 WebAPI, 没有加认证
最简单的代码, Java端,用StringEntity
结果:
但是同样的Json, 使用C#的HTTPClient就完全没有问题.
计较了一下fiddle抓图:
使用C# HttpClient: (value是能取到)
使用Java Apache HttpClient (Value取不到)
比较一下就发现, 在Raw视图, 才可以看出, HttpRequest的Body是有不同的. 就是一对引号. "".
所以把Java的代码中加一行:
str ="\"" +str + "\"";
WebAPI端就正常拿到Value了.
估计有更深层次的解决方案, 暂且先记一笔.
>>2015/06/15
把WebAPI的参数value的类型改为object就可以了. 这么狗血的解决方案.
WebAPI, Android, Apache HttpComponent/HttpClient
问题:无论怎么测试, WebAPI的FormBody value总是空.
最简单的代码 WebAPI, 没有加认证
// POST api/<controller> [HttpPost] public string Post([FromBody]string value) { string val = value; Debug.WriteLine(value); return value; }
最简单的代码, Java端,用StringEntity
public static void NotAuthTestPost() throws IOException { CloseableHttpClient httpclient = HttpClients.createDefault(); try { HttpPost hp = new HttpPost("http://localhost:8888/api/test"); String str="{'Header':[{'summary':111,'endtime':570,'end_date':1431381600,'resid':'sc20150512034635','starttime':570,'scheduleid':'sc155485e5025f3d','machid':'sc155485e8044b93','parentid':null,'start_date':1431381600}],'Detail':[{'perm_modify':1,'owner':1,'perm_delete':1,'resid':'sc20150512034635','memberid':'sc1555070e4d8217','invited':0},{'perm_modify':0,'owner':0,'perm_delete':0,'resid':'sc20150512034635','memberid':'sc155489a07e0e24','invited':1}]}"; StringEntity se = new StringEntity(str, ContentType.create("application/json", "UTF-8")); hp.setEntity(se); System.out.println(EntityUtils.toString(se)); CloseableHttpResponse response1 = null; try { response1 = httpclient.execute(hp); System.out.println(response1.getStatusLine()); HttpEntity entity1 = response1.getEntity(); // do something useful with the response body // and ensure it is fully consumed // EntityUtils.consume(entity1); String jsonstr = EntityUtils.toString(entity1); System.out.println(jsonstr); } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { response1.close(); } } finally { httpclient.close(); } }
结果:
但是同样的Json, 使用C#的HTTPClient就完全没有问题.
计较了一下fiddle抓图:
使用C# HttpClient: (value是能取到)
使用Java Apache HttpClient (Value取不到)
比较一下就发现, 在Raw视图, 才可以看出, HttpRequest的Body是有不同的. 就是一对引号. "".
所以把Java的代码中加一行:
str ="\"" +str + "\"";
WebAPI端就正常拿到Value了.
估计有更深层次的解决方案, 暂且先记一笔.
>>2015/06/15
把WebAPI的参数value的类型改为object就可以了. 这么狗血的解决方案.
相关文章推荐
- 使用asx3m与xstream配合解决flex与java利用httpservice传递xml数据问题
- 使用asx3m与xstream配合解决flex与java利用httpservice传递xml数据问题
- 使用asx3m与xstream配合解决flex与java利用httpservice传递xml数据问题
- java http 客户端使用TSLv1.2[解决Remote host closed connection during handshake的问题]
- jmeter 使用beanshell 调用 java 代码解决请求签名相关问题
- java面向对象-使用this语句解决构造器重载相互调用问题
- 使用Tomcat实现Java连接池(解决main中不能够调用问题)
- C#调用exe解决PDF转图片问题(使用PDFBox方案,java语言编译jar包实现)
- Java中Process和Runtime()使用,以及调用cmd命令阻塞在process.waitfor( )的问题解决
- 解决使用WINHTTP时的一些编译通不过的问题
- Xcode7 不能使用http网络请求问题解决
- 使用install4j打包java项目教程及一些小问题的解决
- 解决Xcode 7 http无法使用的问题
- 解决Java使用c3p0连接池连接mysql数据库出现MESSAGE: Software caused connection abort: socket write error的问题
- jquery1.8版本使用ajax实现微信调用出现的问题分析及解决办法
- crontab无法调用java的问题解决
- 解决spring使用动态代理类型转换失败的问题--java.lang.ClassCastException: com.sun.proxy.$Proxy$ cannot be cast to ...
- java中使用阻塞队列解决生产者消费者问题
- java动态加载指定的类或者jar包反射调用其方法-涉及其他jar中的类就报ClassNotFound问题分析及解决思路
- HttpClient调用webApi时注意的小问题