httpclient提交表单 访问接口
来源:互联网 发布:优米网域名交易 编辑:程序博客网 时间:2024/05/16 10:11
http://shendixiong.iteye.com/blog/1135633
httpclient提交表单 访问接口
- public static String getStringURLtoPost(String url,
- Map<String, Object> parms) {
- if ("".equals(url) || parms == null || url.trim().length() <= 0
- || parms.size() <= 0) {
- return null;
- }
- HttpClient httpClient = new HttpClient();
- PostMethod postMethod = new PostMethod(url); //<strong><span style="color: #ff0000;">此处为你访问的接口 或页面</span>
- </strong>
- //<strong><span style="color: #ff0000;">设置编码 防止中文乱码</span>
- </strong>
- httpClient.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET,"UTF-8");
- //<strong><span style="color: #ff0000;">此处为集合里面的值!遍历map集合中的key和value!其实,这里是关键!这里就是表单数据</span>
- </strong>
- NameValuePair[] data = new NameValuePair[parms.keySet().size()];
- Iterator it = parms.entrySet().iterator();
- int i = 0;
- while (it.hasNext()) {
- Map.Entry entry = (Map.Entry) it.next();
- Object key = entry.getKey();
- Object value = entry.getValue();
- data[i] = new NameValuePair(key.toString(), value.toString());
- i++;
- }
- postMethod.setRequestBody(data); //<strong><span style="color: #ff0000;">这里就是将值传过去(接口中)!</span>
- </strong>
- try {
- httpClient.executeMethod(postMethod); //<strong><span style="color: #ff0000;">执行</span>
- </strong>
- return postMethod.getResponseBodyAsString(); <span style="color: #ff0000;"><strong>//得到接口中返回的数据</strong>
- </span>
- } catch (Exception e) {
- e.printStackTrace();
- } finally{
- postMethod.releaseConnection(); <strong><span style="color: #ff0000;">//关闭(销毁</span>
- </strong>
- }
- return null;
- }
- public static int getCheckInUrl(VideoAsset asset,String username,String text_comments){
- int Checkin_status = CHECKIN_STATUS_FAIL;
- StringBuffer buffer = new StringBuffer();
- Map<String, Object> parms = new HashMap<String, Object>();
- buffer.append("127.0.0.1:82/webservice/sign.jsp?action=add");
- <span style="color: #ff0000;"><strong>//其实.这就相当于,我们的初始页面!然后,下面的数据就是table表单要提交的字段!</strong>
- </span>
- if(asset == null){
- Checkin_status = CHECKIN_STATUS_FAIL;
- }if(asset.getAssetId()>=0){
- parms.put("assetid", asset.getAssetId());
- }if(asset.getItemId()>=0){
- parms.put("itemid", asset.getItemId());
- }if(!"".equals(asset.getAssetType().trim())&&asset.getAssetType()!=null){
- parms.put("assettype", asset.getAssetType().trim());
- }if(!"".equals(asset.getAssetCode().trim())&&asset.getAssetCode()!=null){
- parms.put("assetcode", asset.getAssetCode().trim());
- }if(!"".equals(username.trim())&&username !=null){
- parms.put("username", username.trim());
- }if(!"".equals(asset.getAssetName().trim())&&asset.getAssetName()!=null){
- parms.put("assetname", asset.getAssetName().trim());
- }if(!"".equals(text_comments.trim())&&text_comments!=null){
- parms.put("text_comments", text_comments);
- }
- <span style="color: #ff0000;"><strong>//将要提交的字段数据放入进去!</strong>
- </span>
- return getFromUrltoPost(buffer.toString(), parms);
- }
- public static int getFromUrltoPost(String url,Map<String, Object> parms){
- int Checkin_status = CHECKIN_STATUS_FAIL;
- try {
- Checkin_status = Integer.parseInt(StringUtil.getStringURLtoPost(url, parms));
- return Checkin_status;
- } catch (NumberFormatException e) {
- Checkin_status = CHECKIN_STATUS_UNKNOWNUSERNAME;
- return Checkin_status;
- }finally{
- return Checkin_status;
- }
- }
- <strong><span style="color: #ff0000;">/*
- *最后要告诉大家的是!如果不懂,可以试想成,你做一个简单的登陆页面!里面有name和password哪个字段!
- *而假设你的访问页面是http://127.0.0.1:82/hello.jsp
- *你用post方式!那么你在集合里面就应该put两个,一个是name,一个是password
- *也就是put("name",name) ,put("password",password); 也访问的页面就是你的url(道理就相当于一个
- *简易的登陆提交)
- */</span>
- </strong>
-
0 0
- httpclient提交表单 访问接口
- httpClient 模拟表单提交
- httpclient访问接口
- HttpClient 的 Post 提交表单简单示例
- android中使用httpclient提交表单
- HttpClient 4.1.3模拟表单提交示例
- android中使用httpclient提交表单
- 使用httpclient模拟表单提交,上传图片
- android中使用httpclient提交表单
- Android 使用HttpClient提交表单数据
- 使用httpclient提交表单方式上传文件
- 访问页面表单自动提交
- android利用httpclient实现post、get请求restful接口进行json和form表单数据提交等公共方法类
- Android HttpPost表单访问接口
- HttpClient忽略证书访问HTTPS接口
- httpClient访问http的rest接口
- HttpClient忽略证书访问HTTPS接口
- HttpClient忽略证书访问HTTPS接口()
- android中跳转相册,相机获取图片后截取图片并保存SDCARD
- (转)ARM协处理器CP15寄存器详解
- android studio中gradle版本问题的解决方案
- Matlab的bsxfun函数
- EndNote X7 添加到word/office标题栏的设置
- httpclient提交表单 访问接口
- IOS KeyChain
- TextView如何显示丰富的文本
- 利用URLRewriter重写url地址
- 网络:配置 Apache 服务器
- Logistic回归总结
- 设计模式---工厂模式
- android 5.0后对于apk 跑32 64 的逻辑
- linux文件权限管理