tags:

views:

2934

answers:

9

How do you do a Remote HTTP Post (request) in CSharp?

i really needs this pls. :(

A: 

HttpWebRequest

CodeMonkey1
+4  A: 

You can use WCF or create a WebRequest

var httpRequest = (HttpWebRequest)WebRequest.Create("http://localhost/service.svc");
var httpRequest.Method = "POST";

using (var outputStream = httpRequest.GetRequestStream())
{
    // some complicated logic to create the message
}

var response = httpRequest.GetResponse();
using (var stream = response.GetResponseStream())
{
    // some complicated logic to handle the response message.
}
bendewey
+3  A: 

I use this very simple class:

 public class   RemotePost{
     private  System.Collections.Specialized.NameValueCollection Inputs 
     = new  System.Collections.Specialized.NameValueCollection() ;

    public string  Url  =  "" ;
    public string  Method  =  "post" ;
    public string  FormName  =  "form1" ;

    public void  Add( string  name, string value ){
        Inputs.Add(name, value ) ;
     }

     public void  Post(){
        System.Web.HttpContext.Current.Response.Clear() ;

         System.Web.HttpContext.Current.Response.Write( "<html><head>" ) ;

         System.Web.HttpContext.Current.Response.Write( string .Format( "</head><body onload=\"document.{0}.submit()\">" ,FormName)) ;

         System.Web.HttpContext.Current.Response.Write( string .Format( "<form name=\"{0}\" method=\"{1}\" action=\"{2}\" >" ,

        FormName,Method,Url)) ;
            for ( int  i = 0 ; i< Inputs.Keys.Count ; i++){
            System.Web.HttpContext.Current.Response.Write( string .Format( "<input name=\"{0}\" type=\"hidden\" value=\"{1}\">" ,Inputs.Keys[i],Inputs[Inputs.Keys[i]])) ;
         }
        System.Web.HttpContext.Current.Response.Write( "</form>" ) ;
         System.Web.HttpContext.Current.Response.Write( "</body></html>" ) ;
         System.Web.HttpContext.Current.Response.End() ;
     }
}

And you use it thusly:

RemotePost myremotepost   =  new   RemotePost()  ;
myremotepost.Url  =  "http://www.jigar.net/demo/HttpRequestDemoServer.aspx" ;
myremotepost.Add( "field1" , "Huckleberry" ) ;
myremotepost.Add( "field2" , "Finn" ) ;
myremotepost.Post() ;

Very clean, easy to use and encapsulates all the muck. I prefer this to using the HttpWebRequest and so forth directly.

BobbyShaftoe
Why is this getting downvoted?
BobbyShaftoe
If I'm reading this correctly, it doesn't actually post a form, but responds with a form that can be posted.
David
CodeMonkey1
+2  A: 

Use the WebRequest.Create() and set the Method property.

Eclipse
+1  A: 
HttpWebRequest HttpWReq = 
(HttpWebRequest)WebRequest.Create("http://www.google.com");

HttpWebResponse HttpWResp = (HttpWebResponse)HttpWReq.GetResponse();
Console.WriteLine(HttpWResp.StatusCode);
HttpWResp.Close();

Should print "OK" (200) if the request was successful

John T
Since the OP is doing a POST you should mention the request stream side as well.
bendewey
+1  A: 

Also System.Net.WebClient

Joel Coehoorn
+5  A: 
David
Thanks, the details on how to build the POST data really helped!
Liam
+1  A: 

Im using the following piece of code for calling webservices using the httpwebrequest class:

internal static string CallWebServiceDetail(string url, string soapbody, 
int timeout) {
    return CallWebServiceDetail(url, soapbody, null, null, null, null, 
null, timeout);
}
internal static string CallWebServiceDetail(string url, string soapbody, 
string proxy, string contenttype, string method, string action, 
string accept, int timeoutMilisecs) {
    var req = (HttpWebRequest) WebRequest.Create(url);
    if (action != null) {
     req.Headers.Add("SOAPAction", action);
    }
    req.ContentType = contenttype ?? "text/xml;charset=\"utf-8\"";
    req.Accept = accept ?? "text/xml";
    req.Method = method ?? "POST";
    req.Timeout = timeoutMilisecs;
    if (proxy != null) {
     req.Proxy = new WebProxy(proxy, true);
    }

    using(var stm = req.GetRequestStream()) {
     XmlDocument doc = new XmlDocument();
     doc.LoadXml(soapbody);
     doc.Save(stm);
    }
    using(var resp = req.GetResponse()) {
     using(var responseStream = resp.GetResponseStream()) {
      using(var reader = new StreamReader(responseStream)) {
       return reader.ReadToEnd();
      }
     }
    }
}

This can be easily used to call a webservice

public void TestWebCall() {
    const string url = 
"http://www.ecubicle.net/whois_service.asmx/HelloWorld";
    const string soap = 
@"<soap:Envelope xmlns:soap='about:envelope'>
    <soap:Body><HelloWorld /></soap:Body>
</soap:Envelope>";
    string responseDoc = CallWebServiceDetail(url, soap, 1000);
    XmlDocument doc = new XmlDocument();
    doc.LoadXml(responseDoc);
    string response = doc.DocumentElement.InnerText;
}
Christian Ernst Rysgaard
A: 

The problem when beginning with high-level language like C#, Java or PHP is that people may have never known how simple the underground is in reality. So here’s a short introduction:

http://reboltutorial.com/blog/raw-http-request/

Rebol Tutorial