Local IIS testing, Post calling interface, iispost Calling Interface
I have recently learned three methods to call interfaces: POST, Socket, and Webserivce. I just wrote the POST method today, so I will share it with you. Thank you for your advice.
Public string strResult = "";
Protected void Page_Load (object sender, EventArgs e)
{
MyResponseList ("sent successfully ");
}
Public void MyResponseList (string Charset)
{
Try
{
ASCIIEncoding encoding = new ASCIIEncoding ();
Byte [] byteArray = encoding. GetBytes (Charset );
// The entry address can contain parameters.
HttpWebRequest webReq = (HttpWebRequest) WebRequest. Create (new Uri ("http: // 192.168.16.39: 808/Default. aspx? Num = 3 & name = broueli "));
WebReq. Method = "POST ";
WebReq. ContentType = "application/x-www-form-urlencoded ";
WebReq. ContentLength = byteArray. Length;
// Obtain the request object
Stream newStream = webReq. GetRequestStream ();
NewStream. Write (byteArray, 0, byteArray. Length); // Write Parameters
NewStream. Close ();
// Return Internet response
HttpWebResponse response = (HttpWebResponse) webReq. GetResponse ();
String encod = response. ContentEncoding;
// Determine whether the encoding method is obtained
If (encod = null | encod. Length <1)
{
Encod = "UTF-8 ";
}
// Read the stream. The stream is used to read the response body from the server. Encoding can be directly defined or obtained.
StreamReader sr = new StreamReader (response. GetResponseStream (), Encoding. GetEncoding (encod ));
// Read from the current position of the stream to the end
StrResult = sr. ReadToEnd ();
Sr. Close ();
Response. Close ();
NewStream. Close ();
}
Catch (Exception exp)
{
StrResult = "error:" + exp. Message;
}
}