java 调用 C# webapi
最近项目需要 java调用 .net 的webapi。
对于get来说很简单,但是post方法遇到了些问题,最后也是曲线救国。
先看代码
Java 代码
public static void main(String[] args) throws Exception {
//DoGet(String url)
String resultGet = DoGet("http://localhost:14248/api/Report/GetTest?parentid=40");
//DoPost(String url,List<NameValuePair> nvps)
List<NameValuePair> nvps = new ArrayList<NameValuePair>();
nvps.add(new BasicNameValuePair("parentid", "40"));
String resultPost = DoPost("http://localhost:14248/api/Report/PostTest",nvps);
//DoPost(String url, JSONObject template)
JSONObject template = new JSONObject();
template.put("data",data);
String json=HttpWebapi.DoPost(urlString,template);
//DoPost(String url, String json)
SysUser user=new SysUser();
user.setAccount(etAccount.getText().toString());
user.setPassword(etPwd.getText().toString());
ApiRequest<SysUser> request=new ApiRequest<>();
request.setData(user);
String data=JsonConvert.toJSON(request);
String json = HttpWebapi.DoPost(AppConfig.getUrl() + "api/user/login", data);
}
public static String DoGet(String url) throws Exception {
HttpGet httpGet = new HttpGet(url);
HttpClient client = new DefaultHttpClient();
HttpResponse resp = client.execute(httpGet);
HttpEntity he = resp.getEntity();
String respContent = EntityUtils.toString(he, "UTF-8");
return respContent;
}
public static String DoPost(String url,List<NameValuePair> nvps) throws Exception {
HttpPost httpost = new HttpPost(url);
HttpClient client = new DefaultHttpClient();
httpost.setEntity(new UrlEncodedFormEntity(nvps, "UTF-8"));
HttpResponse resp = client.execute(httpost);
HttpEntity he = resp.getEntity();
String respContent = EntityUtils.toString(he, "UTF-8");
return respContent;
}
public static String DoPost(String url, JSONObject template)throws Exception{
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
httpPost.addHeader(HTTP.CONTENT_TYPE, "application/json");
httpPost.setEntity(new StringEntity(template.toString(),"utf-8"));
HttpResponse resp = httpClient.execute(httpPost);
String respContent = EntityUtils.toString(resp.getEntity(), "UTF-8");
return respContent;
}
public static String DoPost(String url, String json) throws Exception{
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
httpPost.addHeader(HTTP.CONTENT_TYPE, "application/json");
httpPost.setEntity(new StringEntity(json,"utf-8"));
HttpResponse resp = httpClient.execute(httpPost);
String respContent = EntityUtils.toString(resp.getEntity(), "UTF-8");
return respContent;
}
C# WebApi
//public static String DoGet(String url)
[HttpGet]
public dynamic GetTest(int parentId)
{
APIResult<dynamic> result = new APIResult<dynamic>();
result.Data = report.Get_SanJu_ReportSiteCount(parentId);
result.ResponseResult = true;
result.ResponseMsg = "success";
result.ResponseCode = HttpStatusCode.OK; return result;
} //public static String DoPost(String url, List<NameValuePair> nvps)
[HttpPost]
public dynamic PostTest()
{
//通过[FormBody]并不能从方法参数上获得parentId,所以直接从 Request.Form获取
int parentId = int.Parse(HttpContext.Current.Request.Form["parentId"]);
APIResult<dynamic> result = new APIResult<dynamic>();
result.Data = report.Get_SanJu_ReportDesulphurizationTypeByParentId(parentId);
result.ResponseResult = true;
result.ResponseMsg = "success";
result.ResponseCode = HttpStatusCode.OK; return result;
}
Java WebApi
//public static String DoPost(String url, JSONObject template) @Data
public class ApiRequest<T> {
private T data;
private Integer pageIndex = 1;
private Integer pageSize;
} @RequestMapping(value = "test", method = RequestMethod.POST)
public ApiResult<String> test(String test, @RequestBody ApiRequest<String> requestVo) {
ApiResult<String> r = new ApiResult<String>(); r.setData(requestVo.getData());
r.setCodeToSuccessed();
return r;
}
//public static String DoPost(String url, String json) @RequestMapping(value = "login", method = RequestMethod.POST)
public ApiResult<SysUser> login(@RequestBody ApiRequest<SysUser> requestVo) {
ApiResult<SysUser> r = new ApiResult<SysUser>();
SysUser entity = requestVo.getData();
String source = requestVo.getSource();
String platformUUID = entity.getPlatformUUID();
SysUser u = service.getByAccount(entity.getAccount());
// 验证登录成功
if (u != null && u.getPassword().equals(MD5.md5(entity.getPassword()))) {
if(u.getIsValid()==1){
u.setPassword("");//把密码设置为空
r.setData(u);
r.setCodeToSuccessed();
}else {
r.setMessage("The account has been locked!");
}
}else{
r.setMessage("Account or password invalid!");
} return r;
}
java 调用 C# webapi的更多相关文章
- java调用百度地图API依据地理位置中文获取经纬度
百度地图api提供了非常多地图相关的免费接口,有利于地理位置相关的开发,百度地图api首页:http://developer.baidu.com/map/. 博主使用过依据地理依据地理位置中文获取经纬 ...
- JAVA调用 keytool 生成keystore 和 cer 证书
keytool是一个Java数据证书的管理工具, keytool将密钥(key)和证书(certificates)存在一个称为keystore的文件中在keystore里, 包含两种数据: 密钥实体( ...
- java调用mysql服务做备份与恢复
首先添加mysql的bin到环境变量,这样可以简写部分命令,并且做到不依赖系统mysql的具体安装路径. 重启计算机可以让添加的环境变量在java代码中调用时生效.(cmd中生效但java中调用没有生 ...
- 存储过程详解与java调用(转)
存储过程的一些基本语法: --------------创建存储过程----------------- CREATE PROC [ EDURE ] procedure_name [ ; number ] ...
- 调用DiscuzNT webApi 注册 登录 发帖
注册.登录Discuz论坛比较简单,网上很多教程. 3.发帖出现的问题 1.iis8.0版本 asp.net 4.0 不能发帖 将discuz 的web.config文件里的 此代码 <htt ...
- c++ c# java 调用 c++ 写的dll
1. vs 中新建win32 dll 项目 testdll 添加实现文件 test.cpp #include "stdafx.h" #include <ios ...
- Java调用第三方dll文件的使用方法 System.load()或System.loadLibrary()
Java调用第三方dll文件的使用方法 public class OtherAdapter { static { //System.loadLibrary("Connector") ...
- 【转】java调用webservice
互联网上面有很多的免费webService服务,我们可以调用这些免费的WebService服务,将一些其他网站的内容信息集成到我们的Web应用中显示,下面就以获取天气预报数据和查询国内手机号码归属地为 ...
- 分享:写了一个 java 调用 C语言 开发的动态库的范例
分享:写了一个 java 调用 C语言 开发的动态库的范例 cfunction.h 代码#pragma once#ifdef __cplusplusextern "C" {#e ...
随机推荐
- Android自定义布局的背景在多分辨率的情况下设置fill_parent时背景不能够横向全屏的问题解决
问题描述:最近做了一个自定义的控件LinearLayout就是公用的底部菜单条,在指定分辨率下(例如:480x800,480x854)下背景是正常的,但是当程序运行到非指定(默认)的分辨率下就不正常了 ...
- 几个PHP读取整个文件的函数readfile()、fpassthru()和file()
2.7.4 读取整个文件:readfile().fpassthru()和file()除了可以每次读取文件一行外,还可以一次读取整个文件.PHP提供了4种不同的方式来读取整个文件.第一种方式是rea ...
- 03-Centos7安装部署Mirrorgate-踩坑记录
FAQ 1.没有安装bzip2 解决方法 yum -y install bzip2 > phantomjs-prebuilt@2.1.16 install /root/test/mirrorga ...
- [Lydsy1805月赛]对称数 BZOJ5361
分析: 这个题,还是蛮有趣的.考虑,如果l,r区间内的所有数出现奇数次,那么[l-1,r]的抑或和等于所得抑或和. 之后怎么维护呢,主席树维护区间抑或和,记得将每个点附上一个ull级别的随机数,之后抑 ...
- 20155328 网络攻防 实验五:MSF基础应用
20155328 网络攻防 实验五:MSF基础应用 实践内容及过程记录 一.Windows服务渗透攻击----ms08_067 攻击机:kali 靶机:WindowsXP(英文版) 第一步,分别查看攻 ...
- 20155334 曹翔 《网络对抗》逆向及Bof基础
20155334 曹翔 <网络对抗>逆向及Bof基础 实践目标: 本次实践的对象是一个名为pwn1的linux可执行文件. 该程序正常执行流程是:main调用foo函数,foo函数会简单回 ...
- PHP调用WCF提供的方法
一.准备工作 1.安装wampserver:过程略 2.配置wampserver: 2.1打开php.ini文件,去掉 ;extension=php_soap.dll 这里那个分号. 也有说把这个 ; ...
- BigDecimal工具类
package config_service.slowcity; import java.math.BigDecimal; public class ConfigServerApp { /* * 小数 ...
- JVM技术周报第2期
JVM技术周报第2期 JVM技术周报分享JVM技术交流群的讨论内容,由群内成员整理归纳而成.如果你有兴趣入群讨论,请关注「Java技术精选」公众号,通过右下角菜单「入群交流」加我好友,获取入群详情. ...
- CSS 天坑 I - 字体单位
首先,本文所讨论的“坑”是在做回应式网页设计( Responsive Web Design 以下简称 RWD)时显现的,如果你还只是在做传统的Web设计这算不上是一个坑,因为传统的Web页面是死的,不 ...