HTTP Post from Windows Forms application C#

泪湿孤枕 提交于 2019-12-01 11:59:20

问题


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 it possible at all ?

Thanks in advance, Gal.


回答1:


As a start please see

1- HttpWebRequest Class

2- HttpWebResponse Class

3- WebClient Class

in MSDN

Please see Here




回答2:


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.



来源:https://stackoverflow.com/questions/4302173/http-post-from-windows-forms-application-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!