예제 #1
0
        private static string GetPingBackServerURI(WebHeaderCollection headers, string pageText)
        {
            string pingBackServerURI = null;

            // First look for the X-Pingback HTTP Header
            if (headers != null && headers.HasKeys())
            {
                foreach (string key in headers.Keys)
                {
                    if (key.ToLower().Trim() == "x-pingback")
                    {
                        pingBackServerURI = headers[key];
                        break;
                    }
                }
            }

            // If the HTTP header was not found, look for the PingBack <Link> element in the body
            if (string.IsNullOrEmpty(pingBackServerURI) && !string.IsNullOrEmpty(pageText))
            {
                Match m = pingbackLinkElementRegex.Match(pageText);
                if (m.Success)
                    pingBackServerURI = m.Result("$1");
            }

            return pingBackServerURI;
        }
예제 #2
0
        private static void ExectueMethod(String username, String password, String caldavUrl, String methodName, WebHeaderCollection headers, string content, string contentType)
        {
            //            <?xml version="1.0" encoding="utf-8"?>
            //<propfind xmlns="DAV:">
            //  <allprop/>
            //</propfind>

            // Create an HTTP request for the URL.
            HttpWebRequest httpGetRequest =
               (HttpWebRequest)WebRequest.Create(caldavUrl);

            // Set up new credentials.
            httpGetRequest.Credentials =
               new NetworkCredential(username, password);

            // Pre-authenticate the request.
            httpGetRequest.PreAuthenticate = true;

            // Define the HTTP method.
            httpGetRequest.Method = methodName;

            // Optional, but allows for larger files.
            httpGetRequest.SendChunked = true;

            // Specify the request for source code.
            //httpGetRequest.Headers.Add(@"Translate", "F");
            if (headers != null && headers.HasKeys())
                httpGetRequest.Headers = headers;

            byte[] optionsArray = Encoding.UTF8.GetBytes(content);
            httpGetRequest.ContentLength = optionsArray.Length;
            if(!String.IsNullOrWhiteSpace(contentType))
                httpGetRequest.ContentType = contentType;

            // Retrieve the request stream.
            Stream requestStream =
               httpGetRequest.GetRequestStream();

            // Write the string to the destination as a text file.
            requestStream.Write(optionsArray, 0, optionsArray.Length);

            // Close the request stream.
            requestStream.Close();

            // Retrieve the response.
            HttpWebResponse httpGetResponse =
               (HttpWebResponse)httpGetRequest.GetResponse();

            // Retrieve the response stream.
            Stream responseStream =
               httpGetResponse.GetResponseStream();

            // Retrieve the response length.
            long responseLength =
               httpGetResponse.ContentLength;

            // Create a stream reader for the response.
            StreamReader streamReader =
               new StreamReader(responseStream, Encoding.UTF8);
            StringBuilder sb = new StringBuilder();
            // Write the response status to the console.
            sb.AppendFormat(
               @"GET Response: {0}",
               httpGetResponse.StatusDescription).AppendLine();
            sb.AppendFormat(
               @"  Response Length: {0}",
               responseLength).AppendLine();
            sb.AppendFormat(
               @"  Response Text: {0}",
               streamReader.ReadToEnd()).AppendLine();

            // Close the response streams.
            streamReader.Close();
            responseStream.Close();
        }
예제 #3
0
        public static string GetPage(string url, string method, string contentType, SortedList<string, string> values, ref WebHeaderCollection responseHeaders)
        {
            string tmp = string.Empty;
            try
            {
                HttpWebRequest wReq = (HttpWebRequest)WebRequest.Create(url);
                wReq.UserAgent = "FlowLib";

            #if !COMPACT_FRAMEWORK
                wReq.CookieContainer = new CookieContainer();
                // Enable cookie support
                if (responseHeaders != null && responseHeaders.HasKeys())
                {
                    string str = responseHeaders.Get("Set-Cookie");
                    if (!string.IsNullOrEmpty(str))
                    {
                        string[] tmp2= str.Split('=', ' ', ';', ',', '\t', '\n', '\r');

                        if (tmp2.Length >= 2){
                            Cookie cookie = new Cookie(tmp2[0], tmp2[1]);
                            wReq.CookieContainer.Add(wReq.RequestUri, cookie);
                        }
                        //wReq.CookieContainer.Add(new Cookie(
                        //wReq.Headers.Add("Cookie", str);

                        //wReq.Headers.Set("Cookie", str);
                    }
                }
            #endif

                if (!string.IsNullOrEmpty(method) && !string.IsNullOrEmpty(contentType) && values != null)
                {
                    StringBuilder sb = new StringBuilder();
                    foreach (KeyValuePair<string, string> var in values)
                    {
                        sb.Append("&");
                        sb.Append(var.Key);
                        sb.Append("=");
                        sb.Append(var.Value);
                    }
                    byte[] data = Encoding.UTF8.GetBytes(sb.ToString());

                    // Set values for the request back
                    wReq.Method = method;
                    wReq.ContentType = contentType;
                    wReq.ContentLength = data.Length;

                    // Write to request stream.
                    Stream streamOut = wReq.GetRequestStream();
                    streamOut.Write(data, 0, data.Length);
                    streamOut.Flush();
                    streamOut.Close();
                }

                // Get the response stream.
                WebResponse wResp = wReq.GetResponse();
                Stream respStream = wResp.GetResponseStream();
                StreamReader reader = new StreamReader(respStream, Encoding.UTF8);
                tmp = reader.ReadToEnd();

                responseHeaders = wResp.Headers;

                // Close the response and response stream.
                wResp.Close();
            }
            catch (System.Exception e)
            {
                System.Console.WriteLine(e.Message);
            }
            return tmp;
        }