2016-09-07 1 views
0

WebRequest가 Windows CE 프로젝트를 통해 실행 중입니다. 나는 그것을 제대로 보낼 수 있었고 그것이 내 WebApi를 치고있는 것을 볼 수있었습니다. 유일한 문제는 요청이 처리 될 때 매개 변수가 항상 null이라는 것입니다.WebAPI에서 Null로 표시되는 WebRequest POST json 매개 변수

HttpWebRequest req =  

     (HttpWebRequest)WebRequest.Create((uploadUrl.ToString()); 

또한 아래 propertie를 추가 : 예를 들어 다음과 같은 객체를 HttpWebRequest를 만들 때

웹 API

[Route("")] 
    public IHttpActionResult Post([FromBody] StopInfo stopInfo) 
    { 
     try 
     { 
      _scannerService.AddStops(stopInfo); 
      return Ok(stopInfo); 

     } 
     catch (Exception ex) 
     { 
      //return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex); 
      return null; 
     } 
    } 

WebRequest 클래스

private void btnUpload_Click(object sender, EventArgs e) 
    { 

     StopInfo s1 = new StopInfo(); 
     s1.ContactName = "Test"; 
     s1.CompanyName = "Ignite"; 
     s1.City = "Katy"; 
     s1.Addr1 = "22908 Mountain View"; 
     s1.Addr2 = "Suite 300"; 
     s1.State = "TX"; 
     s1.Zip = "77449"; 

     string uploadUrl = txtServerText.Text + "/api/stops"; 

     string json = JsonConvert.SerializeObject(s1); 

     System.Net.WebRequest req = System.Net.WebRequest.Create(uploadUrl); 

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

     byte[] bytes = System.Text.Encoding.ASCII.GetBytes(json); 
     req.ContentLength = bytes.Length; 
     System.IO.Stream os = req.GetRequestStream(); 
     os.Write(bytes, 0, bytes.Length); //Push it out there 
     os.Close(); 
     System.Net.WebResponse resp = req.GetResponse(); 

     System.IO.StreamReader sr = 
       new System.IO.StreamReader(resp.GetResponseStream()); 

    } 

답변

0

봅니다 캐스트를 사용하는 HttpWebRequest 개체에 연결합니다.

req.Timeout = yourWebRequestTimeoutLimit 
    req.KeepAlive = False 
    req.Accept = "application/json" 
    req.Credentials = New NetworkCredential(userName, password)