POST方法:
数据提交
/// <summary> /// POST提交数据接收字符json /// </summary> /// <param name="url">远程服务器路径</param> /// <param name="postData">提交数据</param> /// <returns>接收数据</returns> public static string PostData(string url, byte[] postData) { HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(url); myRequest.Method = "POST"; myRequest.ContentType = "application/x-www-form-urlencoded"; myRequest.ContentLength = postData.Length; Stream newStream = myRequest.GetRequestStream(); // Send the data. newStream.Write(postData, 0, postData.Length); newStream.Close(); // Get response HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse(); StreamReader reader = new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8); return reader.ReadToEnd(); }
调用POST提交结果
public string SendSMS(string mobilenumber,string content) { string Message = ""; //远程提交地址 string RemoteUrl = "http://www.cangcool.com/sms.action?"; string URL = RemoteUrl; string Data = string.Format("u={0}&p={1}&m={2}&c={3}&s={4}&g={5}", _UserName, _Pwd, mobilenumber, content,"",""); byte[] ByteData=System.Text.Encoding.Default.GetBytes(Data); Message = PostData(URL, ByteData); //JavaScriptSerializer serializer = new JavaScriptSerializer(); //jsonOut = serializer.Deserialize<RecordInfo>(strJsonInput); return Message; }
GET方法:
数据提交
/// <summary> /// GET提交 /// </summary> /// <param name="strUrl">远程服务器路径</param> /// <param name="charset">字符编码</param> /// <returns></returns> public static string GeData(string strUrl, string charset) { HttpWebRequest request = (HttpWebRequest)WebRequest.Create(strUrl); request.Method = "GET"; request.Timeout = 60000; HttpWebResponse response = request.GetResponse() as HttpWebResponse; Stream streamReceive = response.GetResponseStream(); Encoding encoding = Encoding.Default; if (!string.IsNullOrEmpty(charset) && Encoding.GetEncoding(charset) != Encoding.Default) { encoding = Encoding.GetEncoding(charset); } StreamReader streamReader = new StreamReader(streamReceive, encoding); return streamReader.ReadToEnd(); }
调用GET提交结果
public string SendSMS(string mobilenumber, string content) { string Message = ""; //远程提交地址 string RemoteUrl = "http://www.cangcool.com/sms.action?"; string Data = string.Format("u={0}&p={1}&m={2}&c={3}&s={4}&g={5}", _UserName, _Pwd, mobilenumber, content, "", ""); string URL = RemoteUrl + Data; Message = GeData(URL,"UTF-8"); return Message; }
时间: 2024-10-29 19:11:32