C# POST application/x-www-form-urlencoded 請求


 

public static string HttpPostFrom(string url, string data) { string htmlAll = ""; try { string SendMessageAddress = url;//請求鏈接
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(SendMessageAddress); request.Method = "POST"; request.AllowAutoRedirect = true; request.Timeout = 20 * 1000; request.ContentType = "application/x-www-form-urlencoded"; request.Headers.Add("x-cherun-auth-key", "LarxMbndsxfGwoYAqsfJSPPU42l04cb3"); //string PostData = "a=1&b=2";//請求參數格式
                string PostData = data;//請求參數
                byte[] byteArray = Encoding.Default.GetBytes(PostData); request.ContentLength = byteArray.Length; using (Stream newStream = request.GetRequestStream()) { newStream.Write(byteArray, 0, byteArray.Length);//寫入參數
 newStream.Close(); } HttpWebResponse response = (HttpWebResponse)request.GetResponse(); Stream rspStream = response.GetResponseStream(); using (StreamReader reader = new StreamReader(rspStream, Encoding.UTF8)) { htmlAll = reader.ReadToEnd(); rspStream.Close(); } response.Close(); } catch (Exception ex) { string s = ex.Message; } return htmlAll; }

 

使用

string data = "appToken=12345258&appKey=dsdsds656s4d6&serviceMethod=gettrack&paramsJson={\"number\":\"" + num + "\"}"; string result = HttpPostFrom(url, data);

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM