c#妯℃嫙Http璇锋眰
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了c#妯℃嫙Http璇锋眰相关的知识,希望对你有一定的参考价值。
鏍囩锛?a href='http://www.mamicode.com/so/1/odi' title='odi'>odi += content web static 瀹炰綋 enc iter amr
涓€銆丳OST璇锋眰
鍙傛暟 paramsValue鐨勬牸寮?瑕佸拰 Reques.ContentType涓€鑷达紝
濡傛灉 contentype "application/x-www-form-urlencoded" 琛ㄥ崟绫诲瀷锛岄偅涔? 鍙傛暟涓? a=1&b=2 褰㈠紡
濡傛灉 contentype "application/json" json 绫诲瀷 閭d箞鍙傛暟灏变负 "a:1,b:2" 鏍煎紡
1.鍙傛暟绫诲瀷锛?ContentType = "application/x-www-form-urlencoded"
锛?锛夊彂璧疯姹傜殑鏂规硶浣?/p>
娉細鍙傛暟鏄嚜瀹氫箟瀹炰綋绫?/p>
public object GetPost([FromBody]RequestTest request) return HttpHelper.HttpPostForm("http://localhost:56188/IDCardOCR/Test",request);
锛?锛夋ā鎷熻姹傚皝瑁?/p>
棣栧厛灏佽鍙傛暟
public static string BuildFrom<T>(T requestFrom) string form = string.Empty; foreach (var property in requestFrom.GetType().GetProperties()) form += property.Name.ToLower() + "=" + property.GetValue(requestFrom).ToString() + "&"; form = form.TrimEnd(鈥?/span>&鈥?/span>); return form;
妯℃嫙璇锋眰瀹炰綋
public static object HttpPostForm(string url, RequestTest requestFrom) string form = BuildFrom<RequestTest>(requestFrom); HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); request.Method = "POST"; request.ContentType = "application/x-www-form-urlencoded"; byte[] paramload = System.Text.Encoding.UTF8.GetBytes(form); request.ContentLength = paramload.Length; Stream writer = request.GetRequestStream(); writer.Write(paramload, 0, paramload.Length); writer.Close(); HttpWebResponse response = (HttpWebResponse)request.GetResponse(); Stream s = response.GetResponseStream(); StreamReader Reader = new StreamReader(s, Encoding.UTF8); object strValue = Reader.ReadLine(); return strValue;
2.鍙傛暟绫诲瀷锛?nbsp;ContentType = "application/json; charset=utf-8"
浠ヤ笂鍑犵妯℃嫙璇锋眰鐨勬帴鏀跺疄浣?/p>
娉細Cup鏄嚜瀹氫箟鎺ユ敹瀹炰綋
[HttpPost("Test")] public int Test(Cup cup) return cup.a + cup.b;
以上是关于c#妯℃嫙Http璇锋眰的主要内容,如果未能解决你的问题,请参考以下文章
ajax璺ㄥ煙璇锋眰webservice webconfig閰嶇疆