C#_调用http服务

HttpClinet调用

using (var client = new HttpClient())
{
	var requestJson = JsonConvert.SerializeObject(new { sap_code = "K00LSC0000004", plant = "3000" });
	HttpContent httpContent = new StringContent(requestJson);
	httpContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");
	client.BaseAddress = new Uri("http://199.234.20.194:8000");               
	var result = client.PostAsync("/sap/mm/mm03", httpContent).Result;
	string resultContent = result.Content.ReadAsStringAsync().Result;
	MessageBox.Show(resultContent);
}

技术要点

  • 传入的参数先用json序列化。(引用网站的需求)。
  • 这里使用了json.net的一个三方包。
  • .NET 4.5才支持 httpclint技术。

HttpWebRequest

string domain = "http://199.234.20.194:8000";
	string url = domain + "/sap/mm/mm03";
	HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
	request.Method = "POST";
	request.ContentType = "application/json";
	//request.ContentType = "application/json";  
	request.ReadWriteTimeout = 30 * 1000;
	
	String postStr = JsonConvert.SerializeObject(new { sap_code = "kk", plant = "3000" });
	byte[] data = Encoding.UTF8.GetBytes(postStr);
	
	request.ContentLength = data.Length;
	
	Stream myRequestStream = request.GetRequestStream();
	myRequestStream.Write(data, 0, data.Length);
	myRequestStream.Close();
	
	HttpWebResponse response = (HttpWebResponse)request.GetResponse();
	StreamReader myStreamReader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
	var retString = myStreamReader.ReadToEnd();
	myStreamReader.Close();
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值