2017-08-01 10 views
2

からPOSTリクエストを送信現在、私は以下のようにcurlコマンドを介してWeb APIに情報を渡す:WindowsフォームのC#

curl -d 'info={ "EmployeeID": [ "1234567", "7654321" ], "Salary": true, "BonusPercentage": 10}' http://example.com/xyz/php/api/createjob.php 

これはあそこにここにすべての情報を掲載APIに私に別のURLを返します。

http://example.com/xyz#newjobapi:id=19

私は、ユーザーに必要な情報を入力しますと、彼らが提出した後、彼らは返されたURLを取得する必要がありますC#のWindowsフォームを通じて、このプロセスを再現しようとしています。

私はすでにユーザーがこの情報を入力するためのインターフェイスを作成しました。しかし、私はどのようにWeb APIにこの情報を投稿し、生成されたURLを取得するかわからない

上記のカールのプロセスをWindowsフォームで複製するために使用できるライブラリはありますか?

+0

あなたはHttpClientクラス –

答えて

1
 HttpWebRequest webRequest; 

     string requestParams = ""; //format information you need to pass into that string ('info={ "EmployeeID": [ "1234567", "7654321" ], "Salary": true, "BonusPercentage": 10}'); 

       webRequest = (HttpWebRequest)WebRequest.Create("http://example.com/xyz/php/api/createjob.php"); 

       webRequest.Method = "POST"; 
       webRequest.ContentType = "application/json"; 

       byte[] byteArray = Encoding.UTF8.GetBytes(requestParams); 
       webRequest.ContentLength = byteArray.Length; 
       using (Stream requestStream = webRequest.GetRequestStream()) 
       { 
        requestStream.Write(byteArray, 0, byteArray.Length); 
       } 

       // Get the response. 
       using (WebResponse response = webRequest.GetResponse()) 
       { 
        using (Stream responseStream = response.GetResponseStream()) 
        { 
         StreamReader rdr = new StreamReader(responseStream, Encoding.UTF8); 
         string Json = rdr.ReadToEnd(); // response from server 

        } 
       } 
関連する問題