c#怎么访问java写得api
2017-07-14 · 百度知道合伙人官方认证企业
育知同创教育
1【专注:Python+人工智能|Java大数据|HTML5培训】 2【免费提供名师直播课堂、公开课及视频教程】 3【地址:北京市昌平区三旗百汇物美大卖场2层,微信公众号:yuzhitc】
向TA提问
关注
展开全部
Encoding code = Encoding.GetEncoding("utf-8");
//待弊神扮请求参数数组字符串
string strRequestData = "";
//把数组转换成流中所需字节数组类型
byte[] bytesRequestData = code.GetBytes(strRequestData);
//构造请求地址
string strUrl = "http://www.xxx.com/api/xxx.do";
//请求远程HTTP
string strResult = "";
try
{
//设置HttpWebRequest基本信息
HttpWebRequest myReq = (HttpWebRequest)HttpWebRequest.Create(strUrl);
myReq.Method = "post";
myReq.ContentType = "application/x-www-form-urlencoded";
//填充POST数据
myReq.ContentLength = bytesRequestData.Length;
Stream requestStream = myReq.GetRequestStream();
requestStream.Write(bytesRequestData, 0, bytesRequestData.Length);
requestStream.Close();
//发送POST数据请求服务器
HttpWebResponse HttpWResp = (HttpWebResponse)myReq.GetResponse();
Stream myStream = HttpWResp.GetResponseStream();
//获取服务器返瞎察回信息
StreamReader reader = new StreamReader(myStream, code);
StringBuilder responseData = new StringBuilder();
String line;
while ((line = reader.ReadLine()) != null)
{
responseData.Append(line);
}
//释租灶放
myStream.Close();
strResult = responseData.ToString();
}
catch (Exception exp)
{
strResult = "报错:" + exp.Message;
}
//待弊神扮请求参数数组字符串
string strRequestData = "";
//把数组转换成流中所需字节数组类型
byte[] bytesRequestData = code.GetBytes(strRequestData);
//构造请求地址
string strUrl = "http://www.xxx.com/api/xxx.do";
//请求远程HTTP
string strResult = "";
try
{
//设置HttpWebRequest基本信息
HttpWebRequest myReq = (HttpWebRequest)HttpWebRequest.Create(strUrl);
myReq.Method = "post";
myReq.ContentType = "application/x-www-form-urlencoded";
//填充POST数据
myReq.ContentLength = bytesRequestData.Length;
Stream requestStream = myReq.GetRequestStream();
requestStream.Write(bytesRequestData, 0, bytesRequestData.Length);
requestStream.Close();
//发送POST数据请求服务器
HttpWebResponse HttpWResp = (HttpWebResponse)myReq.GetResponse();
Stream myStream = HttpWResp.GetResponseStream();
//获取服务器返瞎察回信息
StreamReader reader = new StreamReader(myStream, code);
StringBuilder responseData = new StringBuilder();
String line;
while ((line = reader.ReadLine()) != null)
{
responseData.Append(line);
}
//释租灶放
myStream.Close();
strResult = responseData.ToString();
}
catch (Exception exp)
{
strResult = "报错:" + exp.Message;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询