HTTP Post from Windows Forms application C#

后端 未结 2 603
渐次进展
渐次进展 2021-01-15 21:16

I need to write a forms application in C# that sends parameters via HTTP POST to a url, and gets back the response.

I really don\'t realize where to start this, is i

相关标签:
2条回答
  • 2021-01-15 22:05

    This isn't in C# but you should be able to intepret it.

    var
      Bytes: Array of Byte;
      Request: HttpWebRequest;
      RequestStream: Stream;
      Response: HttpWebResponse;
      ResponseStream: StreamReader;
    begin  
        Bytes := Encoding.UTF8.GetBytes(Data); //Where data is your data (XML in my case)
        Request := WebRequest.CreateDefault(Uri.Create(URL)) as HttpWebRequest;
        Request.Method := 'POST';
        Request.ContentLength := Length(Bytes);
        Request.ContentType := 'application/xml'; //Set accordingly
    
        RequestStream := Request.GetRequestStream;
        RequestStream.Write(Bytes, 0, Length(Bytes));
        RequestStream.Close;
    
        Response := Request.GetResponse as HttpWebResponse;
        ResponseStream := StreamReader.Create(Response.GetResponseStream, Encoding.ASCII);
        Result := ResponseStream.ReadToEnd;
        ResponseStream.Close;
    

    If you need clarification, let me know.

    0 讨论(0)
  • 2021-01-15 22:10

    As a start please see

    1- HttpWebRequest Class

    2- HttpWebResponse Class

    3- WebClient Class

    in MSDN

    Please see Here

    0 讨论(0)
提交回复
热议问题