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璇锋眰的主要内容,如果未能解决你的问题,请参考以下文章

灏忎唬鐮?--http璇锋眰淇℃伅鍐欏叆鏂囦欢

1026 Table Tennis (30鍒?(妯℃嫙)

ajax寮傛璇锋眰

ajax璺ㄥ煙璇锋眰webservice webconfig閰嶇疆

灞呬簬HttpWebRequest鐨凥TTP GET/POST璇锋眰

Fiddler鎶撳彇Java搴旂敤鏈湴鐨凥TTP璇锋眰鎶ユ枃