2017-01-18 8 views
0

I持ちのpythonで、次のHTTP POST要求:変換するために、C#HTTPリクエスト

var tHttpReq = (HttpWebRequest)WebRequest.Create(URL); 
      // adds basic headers 

tHttpReq.ContentType = "application/json"; 
tHttpReq.Method = "POST"; 
tHttpReq.Accept = "application/json"; 
using (StreamWriter sWriter = new StreamWriter(tHttpReq.GetRequestStream())) 
{ 
     // creates request body in the concrete class 
     string rBody = "{\"username\": \"User\", \"password\": \"Password\", \"output_mode\": \"json\"}"; 
     // writes the data to the stream 
     sWriter.Write(rBody); 
     sWriter.Flush(); 
} 
var webHttpResp = (HttpWebResponse)tHttpReq.GetResponse(); 

auth_req = requests.post(baseurl, 
         data={'username': 'User', 'password': 'Password', 'output_mode': 'json'}, verify=False) 

私はへの要求を "変換" しようとしているC#次のように

そして、私は間違った要求のエラーを得ました、何が問題ですか?

ありがとうございました。

+0

ここから来ているwebRequestはどこですか?あなたは、私の代わりにtHttpReqを使うべきです – Newboy

+0

@Newboy、ええ、そのタイプミス、投稿を編集してください。 – CSharpBeginner

答えて

0

HttpClientを使用すると簡単になります。あなたは次のようなことをすることができます:

using (var client = new HttpClient()) 
    { 
     client.BaseAddress = new Uri(@"http://yourEndPoint.com"); 
     HttpContent content = new StringContent("{'json' : 'value'}", Encoding.UTF8, "application/json"); 
     var result = await client.PostAsync("/endpoint", content); 
     var contentAsString = await result.Content.ReadAsStringAsync(); 
    } 
関連する問題