2015-09-07 68 views
2

我有這個問題發送HTTP POST來點網的WebAPI

正與獲取

我想送一個HTTP POST

這裏是我的機器人類,並請求方法

public static String readUrl(String url, ArrayList<NameValuePair> params) { 
    try { 
     HttpClient client = new DefaultHttpClient(); 
     HttpPost method = new HttpPost(url); 

     if (params != null) { 
      method.setEntity(new UrlEncodedFormEntity(params)); 

     } 

     HttpResponse response = client.execute(method); 

     InputStream inputStream = response.getEntity().getContent(); 
     String result = convertInputStreamToString(inputStream); 

     return result; 
    } 
    catch (ClientProtocolException e) { 
     e.printStackTrace(); 
    } 
    catch (IOException e) { 
     e.printStackTrace(); 
    } 

    return null; 
} 

和第是爲請求碼

       ArrayList<NameValuePair> params = new ArrayList<NameValuePair>(); 
           params.add(new BasicNameValuePair("region","1")); 
           params.add(new BasicNameValuePair("area","1")); 
           params.add(new BasicNameValuePair("sector","1")); 
           params.add(new BasicNameValuePair("address",edtReqAddres.getText().toString())); 
           params.add(new BasicNameValuePair("mobile",edtMobileNumber.getText().toString())); 

           params.add(new BasicNameValuePair("username","test")); 
           params.add(new BasicNameValuePair("message", edtSubject.getText().toString())); 
           params.add(new BasicNameValuePair("compid","0")); 
           params.add(new BasicNameValuePair("geo","1")); 


           text = webservice.readUrl("http://192.168.1.102:81/api/products", params); 

而且這是我的結果 :(

{ 「消息」: 「沒有HTTP資源發現,請求URI 'http://192.168.1.102:81/api/products' 相匹配。」}

這是我的WebAPI(DOTNET的)

[System.Web.Http.AcceptVerbs("GET", "POST")] 
    public string GetProductById(int region, int area, int sector, string address, string mobile, string username, string message, int compid, string geo) 
    { 
     fddService.mobService service=new mobService(); 
     return service.NewMessage(region, area, sector, address, mobile, "", message, compid, ""); 
    } 
+0

尚未配置您的服務器來處理'/ API/products'請求。 – Breavyn

+0

嗯我是新的web api你是什麼意思@ColinGillespie請解釋更多 –

+0

我可以通過獲取@ColinGillespie –

回答

0

找到更多的信息,所以最後我到了答案 在客戶端你應該這一行添加到您的代碼來設置defult內容類型

httpPost.addHeader("Content-type", "application/x-www-form-urlencoded"); 

後端服務器(的WebAPI)應該創建一個模型視圖發送PARAMS和也在增加[FromBody]輸入begining PARAMS這樣

public string postreq([FromBody] RequestViewModel model) 
{ 
    fddService.mobService service = new mobService(); 
    if (model.geo == "1") 
     return service.NewMessage(Convert.ToInt32(model.region), Convert.ToInt32(model.area), 
      Convert.ToInt32(model.sector), model.address, model.mobile, "", model.message, Convert.ToInt32(model.compid), ""); 
    else 
     return service.NewMessage(Convert.ToInt32(model.region), Convert.ToInt32(model.area), 
      Convert.ToInt32(model.sector), model.address, model.mobile, "", model.message, Convert.ToInt32(model.compid), model.geo); 
} 
public class RequestViewModel 
{ 
    public string region { get; set; } 
    public string area { get; set; } 
    public string sector { get; set; } 
    public string address { get; set; } 
    public string mobile { get; set; } 
    public string username { get; set; } 
    public string message { get; set; } 
    public string compid { get; set; } 
    public string geo { get; set; } 
} 

,如果你有這個{"Message":"An error has occurred."}

請確認您的Android客戶端正確地將您的參數傳遞給正確的POST代碼。

感謝:地球行星 POST data from Android to Web API returns 404

2

如果您在Visual Studio中使用ASP.NET的WebAPI,你可以添加一個新的Web API控制器類(V2.1),那麼你將有以下默認代碼:

public class ProductsController : ApiController 
    { 
     // GET api/<controller> 
     public IEnumerable<string> Get() 
     { 
      return new string[] { "value1", "value2" }; 
     } 

    // GET api/<controller>/5 
    public string Get(int id) 
    { 
     return "value"; 
    } 

    // POST api/<controller> 
    public void Post([FromBody]string value) 
    { 
    } 

    // PUT api/<controller>/5 
    public void Put(int id, [FromBody]string value) 
    { 
    } 

    // DELETE api/<controller>/5 
    public void Delete(int id) 
    { 
    } 
} 

然後,讓我們假設你有一個DTO類如產品,你可以定製你的Web API,如下所示:

 // POST: api/Products 
     [ResponseType(typeof(Product))] 
     public async Task<IHttpActionResult> PostProduct(Product product) 
     { 
      if (!ModelState.IsValid) 
      { 
       return BadRequest(ModelState); 
      } 

      db.Products.Add(product); 
      await db.SaveChangesAsync(); 

      return CreatedAtRoute("DefaultApi", new { id = product.Id }, product); 
     } 

您將在Learn About ASP.NET Web API

+0

這是完美@BNK非常感謝 –

+0

但你知道我想讓我的方法只接受發佈請求@BNK [FromBody]足夠了嗎? –

+0

'Post'已經是POST了,FromBody只是POST的一個主體:) – BNK