.net post form in code behind

前端 未结 4 487
野性不改
野性不改 2020-12-21 07:06

I wanna make a post form in code behind. I have simple html post is working but when I try make it WebRequest I can\'t make it work.

Thanks for you time in advance.<

相关标签:
4条回答
  • 2020-12-21 07:26

    This might help you.

    string URI = "http://www.myurl.com/post.php";
    string myParamters = "param1=value1&param2=value2";
    
    WebClient wc = new WebClient();
    wc.Headers["Content-type"] = "application/x-www-form-urlencoded";
    string HtmlResult = wc.UploadString(URI, myParameters);
    
    0 讨论(0)
  • 2020-12-21 07:32

    The first thing I see wrong is that you're trying to use a query string to post the form data. Your "form data" should be like so:

     String ali = "datafromuser=<CC5Request><Name>Mert</Name><Password>123</Password><ClientId>xxxx</ClientId><IPAddress>213</IPAddress><Adress>asdsa</Adress>" +
                                "<OrderId>123</OrderId><Type>Auth</Type><Number>1234567891234567</Number><ExpiresAy>01</ExpiresAy><ExpiresYil>13</ExpiresYil><Cvv2Val>123</Cvv2Val>" +
                                "<Total>10</Total><Taksit></Taksit><Kdv>xx</Kdv><BankaID>1</BankaID><TcKimlik>12345678912</TcKimlik></CC5Request>";
    

    Next, you need to get the bytes[] from your form data.

    byte[] byteArray = Encoding.UTF8.GetBytes(ali);
    

    Set some headers:

    req.ContentType = "application/x-www-form-urlencoded"; 
    req.ContentLength = byteArray.Length;
    req.Method = "POST"; 
    

    Now write your data to the request stream.

    Stream dataStream = req.GetRequestStream(); 
    dataStream.Write(byteArray, 0, byteArray.Length); 
    dataStream.Close(); 
    

    Finally... get your response. Also note, anything that implements IDisposable should be wrapped in a using statement, i.e. Stream and WebResponse.

    Also note that the submit button is not part of your form post data. It's possible the server is expecting it.

    Edit: Here's a complete example from Microsoft that guides you step by step.

    http://msdn.microsoft.com/en-us/library/debx8sh9.aspx

    0 讨论(0)
  • 2020-12-21 07:41

    This is simples example. postData depends of form

    String postData = "Name=" + Username +
                  "&Password=" + Password +
                  "&Retype=" + Password +
                  "&Email=" + HttpUtility.UrlEncode(EmailAddress) +
                  "&RealName=" + String.Format("{0}+{1}", FirstName.Replace(" ", "+"), LastName.Replace(" ", "+"));
    

    Now create request and post data:

    HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
    request.Method = WebRequestMethods.Http.Post;
    request.ContentLength = postData.Length;
    request.ContentType = "application/x-www-form-urlencoded";
    request.KeepAlive = false;
    
    StreamWriter writer = new StreamWriter(request.GetRequestStream());
    writer.Write(postData);
    writer.Close();
    HttpWebResponse response = (HttpWebResponse)request.GetResponse();
    StreamReader reader = new StreamReader(response.GetResponseStream());
    String responseString = reader.ReadToEnd();
    
    response.Close();
    

    You can use Fiddler in order to catch how postData looks.

    0 讨论(0)
  • 2020-12-21 07:43

    Try like this.

    using (WebClient client = new WebClient())
       {
    
           byte[] response = client.UploadValues("http://dork.com/service", new NameValueCollection()
           {
               { "home", "Cosby" },
               { "favorite+flavor", "flies" }
           });
       }
    

    You will need these includes:

    using System;
    using System.Collections.Specialized;
    using System.Net;
    

    If you're insistent on using a static method/class:

    public static class Http
    {
        public static byte[] Post(string uri, NameValueCollection pairs)
        {
            byte[] response = null;
            using (WebClient client = new WebClient())
            {
                response = client.UploadValues(uri, pairs);
            }
            return response;
        }
    }
    

    Then simply:

    Http.Post("http://dork.com/service", new NameValueCollection() {
        { "home", "Cosby" },
        { "favorite+flavor", "flies" }
    });
    
    0 讨论(0)
提交回复
热议问题