C#發(fā)送HttpPost請求來調用WebService的方法
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)
{
}
}
//發(fā)送消息到服務器
public string HttpConnectToServer(string ServerPage,string strXml,string strData)
{
string postData = "strXml=" + strXml+"&strData="+strData;
byte[] dataArray = Encoding.Default.GetBytes(postData);
//創(chuàng)建請求
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(ServerPage);
request.Method = "POST";
request.ContentLength = dataArray.Length;
request.ContentType = "application/x-www-form-urlencoded";
//創(chuàng)建輸入流
Stream dataStream = null;
try
{
dataStream = request.GetRequestStream();
}
catch (Exception)
{
return null;//連接服務器失敗
}
//發(fā)送請求
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;
}
相關文章
C#中datagridview的EditingControlShowing事件用法實例
這篇文章主要介紹了C#中datagridview的EditingControlShowing事件用法,實例分析了datagridview的EditingControlShowing事件的定義與使用技巧,需要的朋友可以參考下2015-06-06Unity UGUI的LayoutElement布局元素組件介紹使用示例
這篇文章主要為大家介紹了Unity UGUI的LayoutElement布局元素組件介紹使用示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-07-07在WPF中合并兩個ObservableCollection集合
這篇文章介紹了在WPF中合并兩個ObservableCollection集合的方法,文中通過示例代碼介紹的非常詳細。對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-06-06c# 獲取CookieContainer的所有cookies函數代碼
這篇文章主要介紹了c# 獲取CookieContainer所有cookies的函數代碼,需要的朋友可以參考下2013-06-06