public string WebRequest(HttpMethd method, string url, string postData) { HttpWebRequest webRequest = null; StreamWriter requestWriter = null; string responseData = ""; webRequest = System.Net.WebRequest.Create(url) as HttpWebRequest; webRequest.Method = method.ToString(); webRequest.ServicePoint.Expect100Continue = false; //webRequest.UserAgent = "Identify your application please."; //webRequest.Timeout = 20000; if (method == HttpMethd.POST || method == HttpMethd.PUT) { if (method == HttpMethd.PUT) { webRequest.ContentType = "text/xml"; webRequest.Method = "PUT"; } else { webRequest.ContentType = "application/x-www-form-urlencoded"; //webRequest.ContentType = "multipart/form-data"; } //POST the data. requestWriter = new StreamWriter(webRequest.GetRequestStream()); try { requestWriter.Write(postData); } catch { throw; } finally { requestWriter.Close(); requestWriter = null; } } responseData = WebResponseGet(webRequest); webRequest = null; return responseData; }
private void Initialize() { string[] split = Request.Url.AbsolutePath.DelimitSplit("/", "."); Dictionary <string, object> queryString = new Dictionary <string, object>(); if (Request.QueryString.Count > 0) { foreach (string key in Request.QueryString.Keys) { queryString.Add(key, Request.QueryString[key]); } } Query = queryString; switch (HttpMethd.ToUpperInvariant()) { case "POST": // ** Create / POST (data in request body)** // /{Type}.{ext} if (split.Length == 2) { TypeName = split[0]; Extension = string.Format(".{0}", split[1]); IsValid = true; } ; break; case "GET": // ** Retrieve / GET ** // /{Type}.{ext}?{Query} // /{Type}/{Id}.{ext} // /{Type}/{Id}/{ChildListProperty}.{ext} switch (split.Length) { case 2: TypeName = split[0]; Extension = string.Format(".{0}", split[1]); IsValid = true; break; case 3: long id3; if (long.TryParse(split[1], out id3)) { TypeName = split[0]; Id = id3; Extension = string.Format(".{0}", split[2]); IsValid = true; } break; case 4: long id4; if (long.TryParse(split[1], out id4)) { TypeName = split[0]; Id = id4; ChildListProperty = split[2]; Extension = string.Format(".{0}", split[3]); IsValid = true; } break; default: IsValid = false; break; } break; case "PUT": // ** Update / PUT (data in request body)** // /{Type}/{Id}.{ext} long putId; if (long.TryParse(split[1], out putId)) { TypeName = split[0]; Id = putId; Extension = string.Format(".{0}", split[2]); IsValid = true; } break; case "DELETE": // ** Delete / DELETE // /{Type}/{Id} long deleteId; if (long.TryParse(split[1], out deleteId)) { TypeName = split[0]; Id = deleteId; Extension = string.Format(".{0}", split[2]); IsValid = true; } break; default: IsValid = false; break; } }