private void SendGrapevineSMS(string PagerNumber, string PagerMessage, string Reference) { PagerMessage += " (" + Strings.FormatDateTime(DateAndTime.Now, Constants.vbShortTime) + ")"; try { string dt = Strings.Format(DateAndTime.Now, "yyyy-mm-ddTHH:mm:ss"); string xmlText = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + "<gviSmsMessage xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">" + "<originator>" + My.MySettingsProperty.Settings.GrapeVineAffiliate + "</originator>" + "<affiliateCode>" + My.MySettingsProperty.Settings.GrapeVineAffiliate + "</affiliateCode>" + "<authenticationCode>" + My.MySettingsProperty.Settings.GrapeVineAuthentication + "</authenticationCode>" + "<submitDateTime>" + dt + "</submitDateTime>" + "<messageType>Text</messageType>" + "<recipientList>" + "<message>" + PagerMessage + "</message>" + "<recipient>" + "<msisdn>" + PagerNumber + "</msisdn>" + "<customData>" + "<myRef>" + Reference + "</myRef>" + "</customData>" + "</recipient>" + "</recipientList>" + "</gviSmsMessage>"; HttpWebRequest HttpWReq; HttpWebResponse HttpWResp; byte[] bytes; bytes = Encoding.ASCII.GetBytes(xmlText); string GUrl = My.MySettingsProperty.Settings.GrapeVineUrl.ToString(); HttpWReq = (HttpWebRequest)WebRequest.Create(GUrl); HttpWReq.Method = "POST"; HttpWReq.ContentType = "application/x-www-form-urlencoded"; HttpWReq.ContentLength = bytes.Length; var myWriter = new StreamWriter(HttpWReq.GetRequestStream()); myWriter.Write(xmlText); myWriter.Close(); HttpWResp = (HttpWebResponse)HttpWReq.GetResponse(); var myreader = new StreamReader(HttpWResp.GetResponseStream()); string Retstring = myreader.ReadToEnd(); myreader.Close(); HttpWResp.Close(); } catch (Exception ex) { logger.Error("sendgrapevinesms" + ex.ToString() + " " + PagerNumber + " " + Reference); } }
/// <summary> /// http://forums.devshed.com/c-programming-42/calling-a-url-from-c-371960.html /// </summary> /// <param name="url"></param> public static String GetUrl(String url) { String postData = "parameter=text¶m2=text2"; ASCIIEncoding encoding = new ASCIIEncoding(); byte[] baASCIIPostData = encoding.GetBytes(postData); HttpWebRequest HttpWReq = (HttpWebRequest)WebRequest.Create(url);//"http://www.server.com/page.php"); HttpWReq.Method = "POST"; HttpWReq.Accept = "text/plain"; HttpWReq.ContentType = "application/x-www-form-urlencoded"; HttpWReq.ContentLength = baASCIIPostData.Length; Stream streamReq; streamReq = null; // Prepare web request and send the data. try { streamReq = HttpWReq.GetRequestStream(); } catch (WebException exc) { logger.Debug("WebException " + exc.Message); return(null); } //return null; streamReq.Write(baASCIIPostData, 0, baASCIIPostData.Length); // grab the response HttpWebResponse HttpWResp; HttpWResp = null; try { HttpWResp = (HttpWebResponse)HttpWReq.GetResponse(); } catch (ProtocolViolationException e) { logger.Error(e.Message); return(null); } catch (NotSupportedException e) { logger.Error(e.Message); } Stream streamResponse = HttpWResp.GetResponseStream(); // And read it out StreamReader reader = new StreamReader(streamResponse); String response = reader.ReadToEnd(); return(response); }
public static string getServiceResult(string serviceUrl, string method) { log("Requesting :" + serviceUrl, _appPath); try { HttpWebRequest HttpWReq; HttpWebResponse HttpWResp; HttpWReq = (HttpWebRequest)WebRequest.Create(serviceUrl); CredentialCache cc = new CredentialCache(); HttpWReq.Credentials = CredentialCache.DefaultCredentials; if (!string.IsNullOrEmpty(config.User)) { HttpWReq.Credentials = BuildCredentials(serviceUrl, config.User, config.Password, "NTLM"); log(config.LastErreur, _appPath); } HttpWReq.Method = string.IsNullOrEmpty(method)? "Post":method; HttpWReq.ContentLength = 0; HttpWResp = (HttpWebResponse)HttpWReq.GetResponse(); if (HttpWResp.StatusCode == HttpStatusCode.OK) { //Consume webservice with basic XML reading, assumes it returns (one) string XmlReader reader = XmlReader.Create(HttpWResp.GetResponseStream()); while (reader.Read()) { reader.MoveToFirstAttribute(); if (reader.NodeType == XmlNodeType.Text) { return(reader.Value); } } return(string.Empty); } else { log("Getting response :" + HttpWResp.StatusCode.ToString(), _appPath); throw new Exception("Error on remote : " + HttpWResp.StatusCode.ToString()); } } catch (Exception e) { log("Erreur :" + e.ToString(), _appPath); return(null); } }
private string GetServiceResult(string serviceUrl) { HttpWebRequest HttpWReq; HttpWebResponse HttpWResp; HttpWReq = (HttpWebRequest)WebRequest.Create(serviceUrl); HttpWReq.Method = "GET"; HttpWReq.ContentType = "application/json; charset:utf-8"; HttpWReq.CookieContainer = new CookieContainer(); if (Request.Cookies != null) { for (int i = 0; i < Request.Cookies.Count; i++) { HttpCookie oCookie = Request.Cookies.Get(i); Cookie oC = new Cookie() { Domain = HttpWReq.RequestUri.Host, Expires = oCookie.Expires, Name = oCookie.Name, Path = oCookie.Path, Secure = oCookie.Secure, Value = oCookie.Value }; HttpWReq.CookieContainer.Add(oC); } } //using (var writer = new StreamWriter(HttpWReq.GetRequestStream())) //{ // writer.Write(data); // writer.Flush(); // writer.Close(); //} HttpWResp = (HttpWebResponse)HttpWReq.GetResponse(); if (HttpWResp.StatusCode == HttpStatusCode.OK) { //Consume webservice with basic XML reading, assumes it returns (one) string StreamReader reader = new StreamReader(HttpWResp.GetResponseStream()); return(reader.ReadToEnd()); } else { throw new Exception("Error: " + HttpWResp.StatusCode.ToString()); } }
public static string getServiceResult(string serviceUrl) { HttpWebRequest HttpWReq; HttpWebResponse HttpWResp; HttpWReq = (HttpWebRequest)WebRequest.Create(serviceUrl); HttpWReq.Method = "GET"; HttpWResp = (HttpWebResponse)HttpWReq.GetResponse(); if (HttpWResp.StatusCode == HttpStatusCode.OK) { using (var reader = new StreamReader(HttpWResp.GetResponseStream())) return(reader.ReadToEnd()); } else { throw new ApplicationException("WebServices.getServiceResult error: " + HttpWResp.StatusCode.ToString()); } }
public static string CallWebService(string webServiceUrl, string header = "") { HttpWebRequest HttpWReq; HttpWebResponse HttpWResp; HttpWReq = (HttpWebRequest)WebRequest.Create(webServiceUrl); HttpWReq.Method = "GET"; if (header != "") { HttpWReq.Headers.Add(header); } HttpWResp = (HttpWebResponse)HttpWReq.GetResponse(); if (HttpWResp.StatusCode == HttpStatusCode.OK) { //Consume webservice with basic XML reading, assumes it returns (one) string XmlReaderSettings settings = new XmlReaderSettings(); settings.ProhibitDtd = false; XmlReader reader = XmlReader.Create(HttpWResp.GetResponseStream(), settings); try { while (reader.Read()) { reader.MoveToFirstAttribute(); if (reader.NodeType == XmlNodeType.Text) { return(reader.Value); } } } catch (Exception exception) { return(exception.Message); } return(String.Empty); } else { throw new Exception("Error on remote IP to Country service: " + HttpWResp.StatusCode.ToString()); } }
public string getServiceResult(string serviceUrl) { try { HttpWebRequest HttpWReq; HttpWebResponse HttpWResp; HttpWReq = (HttpWebRequest)WebRequest.Create(serviceUrl); HttpWReq.Method = "GET"; HttpWResp = (HttpWebResponse)HttpWReq.GetResponse(); if (HttpWResp.StatusCode == HttpStatusCode.OK) { StreamReader reader = new StreamReader(HttpWResp.GetResponseStream()); return(reader.ReadToEnd()); } else { throw new Exception("Error on remote IP to Country service: " + HttpWResp.StatusCode.ToString()); } } catch { return("Error"); } }