2017-08-30 10 views
1

マイコード。データを投稿した後でWebClientレスポンスを読み取る方法は? WebClient.UploadDataメソッド(String、文字列、バイト[])ここ

string uriString = "http://www.Testcom"; 
WebClient myWebClient = new WebClient(); 
string postData = "data"; 
myWebClient.Headers.Add("Content-Type","application/x-www-form-urlencoded"); 
Console.WriteLine(myWebClient.Headers.ToString()); 
byte[] byteArray = Encoding.ASCII.GetBytes(postData);   
byte[] responseArray = myWebClient.UploadData(new 
Uri(uriString),"POST",byteArray); 

私は今このようなAPIプロジェクトで作成されたUploadDataとgetメソッドを呼び出します。

[HttpPost] 
[Route("doc2pdf")] 
public HttpResponseMessage doc2pdf(byte[] fileContent) 
{ 
    string pdfContent = string.Empty; 
    //if(string.IsNullOrEmpty(docContent)) 
    //{ 
    // var resp = Request.CreateResponse(HttpStatusCode.BadRequest,"Document content is empty."); 
    // return resp; 
    //} 
    if(fileContent != null || fileContent.Length > 0) 
    { 
     ..logic here 
    } 
} 

問題は常にfileContent get {byte [0]}です。 enter image description here

は今、どのように私は、HTTP出力を読みますか。 WEBAPIにデータを送信するために

答えて

0

好適な方法は、使用してJSONです。フォームエンコードされたデータを使用したい場合しかし、あなたがすべき: public HttpResponseMessage doc2pdf([FromBody]string fileContent)

  • をパラメータを読んで、単純型を使用するようにリクエストボディを使用する

    1. ヒントWebAPIのをリードする「=」記号でデータを送信します。だから、

    、クライアントコード

    string uriString = "http://www.Testcom"; 
    
    WebClient myWebClient = new WebClient(); 
    string postData = "=data"; 
    myWebClient.Headers.Add("Content-Type", "application/x-www-form-urlencoded"); 
    Console.WriteLine(myWebClient.Headers.ToString()); 
    byte[] byteArray = Encoding.ASCII.GetBytes(postData); 
    byte[] responseArray = myWebClient.UploadData(new Uri(uriString), "POST", byteArray); 
    

    サーバー側のコード

    public HttpResponseMessage doc2pdf([FromBody]string fileContent) 
    { 
        //..logic here 
    } 
    
  • 関連する問題