Send Request By Post : Web Request Response « Network « C# / C Sharp






Send Request By Post

        
        
//**************************************************************
//
// MoneyBaby Project - Open source payment processors for .NET
//
// Copyright 2007-2008 Marcus McConnell and BV Software
// www.CodePlex.com/MoneyBaby
//**************************************************************

using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Net;
using System.IO;

namespace BVSoftware.MoneyBaby
{
    public class Utilities
    {

        private const int DEFAULTTIMEOUT = 100000;

        public static string SendRequestByPost(string serviceUrl, string postData)
        {
            return SendRequestByPost(serviceUrl, postData, null, DEFAULTTIMEOUT);
        }

        public static string SendRequestByPost(string serviceUrl, string postData, System.Net.WebProxy proxy, int timeout)
        {
            WebResponse resp = null;
            WebRequest req = null;
            string response = string.Empty;
            byte[] reqBytes = null;

            try
            {
                reqBytes = Encoding.UTF8.GetBytes(postData);
                req = WebRequest.Create(serviceUrl);
                req.Method = "POST";
                req.ContentLength = reqBytes.Length;
                req.ContentType = "application/x-www-form-urlencoded";
                req.Timeout = timeout;
                if (proxy != null)
                {
                    req.Proxy = proxy;
                }
                Stream outStream = req.GetRequestStream();
                outStream.Write(reqBytes, 0, reqBytes.Length);
                outStream.Close();
                resp = req.GetResponse();
                StreamReader sr = new StreamReader(resp.GetResponseStream(), Encoding.UTF8, true);
                response += sr.ReadToEnd();
                sr.Close();
            }
            catch
            {
                throw;
            }

            return response;
        }

    }
}

   
    
    
    
    
    
    
    
  








Related examples in the same category

1.Uses WebRequest and WebResponse. Tests use HTTP and the file protocol
2.Get HTTP Request Headers
3.Get HTTP Response headers
4.Provides a response from a Uniform Resource Identifier (URI).
5.Gets or sets the content length of data being received.
6.Returns the data stream from the Internet resource.
7.Gets a collection of header name-value pairs associated with this request.
8.Boolean value that indicates whether mutual authentication occurred.
9.Gets the URI of the Internet resource that actually responded to the request.
10.Create a new instance of the WebRequest class.
11.Initializes a new WebRequest instance for the specified URI scheme.
12.Create WebRequest instance for the specified URI scheme.
13.Gets or sets the network credentials used for authenticating the request with the Internet resource.
14.Returns a response to an Internet request.
15.Gets or sets the collection of header name/value pairs associated with the request.
16.Indicates whether to pre-authenticate the request.
17.Gets the URI of the Internet resource associated with the request.