POST URL 地址資料開啟URL地址

皑上雪發表於2024-10-17

如下為.net版呼叫例項,關鍵是兩點:1、Post方式;2、引數以JSON格式:

string postUrl = "https://xxx.com/caradigm";

string paramData =

"{\"ex_patientid\":\"10_2000_XXXXXXX\",\"userName\":\"3000419\",\"ex_appid\":\"XXXXXXX\"}";

try

{

byte[] byteArray = Encoding.UTF8.GetBytes(paramData); //轉化

HttpWebRequest webReq = (HttpWebRequest)WebRequest.Create(new Uri(postUrl));

webReq.Method = "POST";

webReq.ContentType = "application/x-www-form-urlencoded";

webReq.ContentLength = byteArray.Length;

Stream newStream = webReq.GetRequestStream();

newStream.Write(byteArray, 0, byteArray.Length);//寫入引數

newStream.Close();

HttpWebResponse response = (HttpWebResponse)webReq.GetResponse();

StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.UTF8);

ret = sr.ReadToEnd();

sr.Close();

response.Close();

newStream.Close();

return ret ; //ret即為URL的地址。

}

catch (Exception ex)

{

}

相關文章