C\#웹 서 비 스 를 호출 하기 위해 HttpPost 요청 을 보 내 는 방법

2347 단어 HttpPostWebService

void UpdateContactSign()
        {
           string ServerPage ="http://localhost/WebService/MyService.asmx";
            try
            {
                //ServerPage += "?op=TangramAction";
                ServerPage += "/MyAction";//MyAction WebService
           string strXml="<a ObjID=\"9\"></a>",;//
           string strData="ContactSign|990011| ";//
           string res = HttpConnectToServer(ServerPage, strXml, strData);
                //MessageBox.Show(res);
            }
            catch (Exception ex)
            {

            }
        }

        //
      public string HttpConnectToServer(string ServerPage,string strXml,string strData)
        {
            string postData = "strXml=" + strXml+"&strData="+strData;

            byte[] dataArray = Encoding.Default.GetBytes(postData);
            //
            HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(ServerPage);
            request.Method = "POST";
            request.ContentLength = dataArray.Length;
            request.ContentType = "application/x-www-form-urlencoded";
            //
            Stream dataStream = null;
            try
            {
                dataStream = request.GetRequestStream();
            }
            catch (Exception)
            {
                return null;//
            }

            //
            dataStream.Write(dataArray, 0, dataArray.Length);
            dataStream.Close();
            //
            string res = string.Empty;
            try
            {
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
                res = reader.ReadToEnd();
                reader.Close();
            }
            catch (Exception ex)
            {
                return null;//
            }
            return res;
        }

좋은 웹페이지 즐겨찾기