1.參數 paramsValue的格式 要和 Reques.ContentType一致,
如果 contentype "application/x-www-form-urlencoded" 表單類型,那么 參數為 a=1&b=2 形式
如果 。。。 "application/json" json 類型 那么參數就為 "{a:1,b:2}" 格式
接收json
Stream postData = Request.InputStream; StreamReader sRead = new StreamReader(postData); string postContent = sRead.ReadToEnd(); sRead.Close();
//body是要傳遞的參數,格式"roleId=1&uid=2" //post的cotentType填寫: //"application/x-www-form-urlencoded" //soap填寫:"text/xml; charset=utf-8" public string PostHttp(string url, string body, string contentType) { HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(url); httpWebRequest.ContentType = contentType; httpWebRequest.Method = "POST"; httpWebRequest.Timeout = 20000; byte[] btBodys = Encoding.UTF8.GetBytes(body); httpWebRequest.ContentLength = btBodys.Length; httpWebRequest.GetRequestStream().Write(btBodys, 0, btBodys.Length); HttpWebResponse httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse(); StreamReader streamReader = new StreamReader(httpWebResponse.GetResponseStream()); string responseContent = streamReader.ReadToEnd(); httpWebResponse.Close(); streamReader.Close(); httpWebRequest.Abort(); httpWebResponse.Close(); return responseContent; }
