唯一的GET方法一直在工作,但PUT、POST和DELETE总是出错。我尝试通过网络更新处理程序映射。也可以在IIS站点下进行配置。最初,我得到了错误的状态代码405作为方法不允许。当我将处理程序映射更改为
<system.webServer>
<handlers>
<remove name="ExtensionlessUrlHandler-Integrated-4.0" />
<remove name="OPTIONSVerbHandler" />
<remove name="TRACEVerbHandler" />
<remove name="WebDAV" />
<remove name="ExtensionlessUrlHandler-Integrated-4.0" />
<add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="*" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
<remove name="ExtensionlessUrl-Integrated-4.0" />
<add name="ExtensionlessUrl-Integrated-4.0"
path="*."
verb="GET,HEAD,POST,DEBUG,DELETE,PUT"
type="System.Web.Handlers.TransferRequestHandler"
preCondition="integratedMode,runtimeVersionv4.0" />
</handlers>
<validation validateIntegratedModeConfiguration="false" />
<modules>
<remove name="ApplicationInsightsWebTracking" />
<add name="ApplicationInsightsWebTracking" type="Microsoft.ApplicationInsights.Web.ApplicationInsightsHttpModule, Microsoft.AI.Web" preCondition="managedHandler" />
<remove name="WebDAVModule"/>
</modules>
开始将415的错误设置为“不支持的媒体类型”。以下是我收到的回复
{StatusCode: 415, ReasonPhrase:'Unsupport Media Type', Version: 1.1, Content: System. Net. Http. StreamContent, Headers:{Cache-Control: no-cache Pragma: no-cache Server: Microsoft-IIS/8.5 X-AspNet-Version: 4.0.30319 X-Powerd-By: ASP. NET Date: Thu,17 Nov2016 16:44:52GMT Content-Type: Application/octet-stream; charset=utf-8 Expires:-1 Content-Llong: 100}}
1.以下是我的建议
// PUT: api/CreditRequests/5
[ResponseType(typeof(void))]
public IHttpActionResult PutCreditRequest(Guid id, CreditRequest creditRequest)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
if (id != creditRequest.CreditRequestId)
{
return BadRequest();
}
db.Entry(creditRequest).State = EntityState.Modified;
try
{
db.SaveChanges();
}
catch (DbUpdateConcurrencyException)
{
if (!CreditRequestExists(id))
{
return NotFound();
}
else
{
throw;
}
}
return StatusCode(HttpStatusCode.NoContent);
}
// POST: api/CreditRequests
[ResponseType(typeof(CreditRequest))]
public IHttpActionResult PostCreditRequest(CreditRequest creditRequest)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
db.CreditRequests.Add(creditRequest);
try
{
db.SaveChanges();
}
catch (DbUpdateException)
{
if (CreditRequestExists(creditRequest.CreditRequestId))
{
return Conflict();
}
else
{
throw;
}
}
return CreatedAtRoute("DefaultApi", new { id = creditRequest.CreditRequestId }, creditRequest);
}
// DELETE: api/CreditRequests/5
[ResponseType(typeof(CreditRequest))]
public IHttpActionResult DeleteCreditRequest(Guid id)
{
CreditRequest creditRequest = db.CreditRequests.Find(id);
if (creditRequest == null)
{
return NotFound();
}
db.CreditRequests.Remove(creditRequest);
db.SaveChanges();
return Ok(creditRequest);
}
我使用HttpClient对象调用它们。代码为
string jsondata = JsonConvert.SerializeObject(item);
var content = new StringContent(jsondata, System.Text.Encoding.UTF8, "application/json");
HttpResponseMessage response = null;
using (var client = GetFormattedHttpClient())// Adding basic authentication in HttpClientObject before using it.
{
if (IsNew == true)
response = client.PostAsync (_webUri, content).Result;
else if (IsNew == false)
response = client.PutAsync(_webUri, content).Result;
}
if (!response.IsSuccessStatusCode)
return false;
else
return true;
这对我有用。
protected async Task<String> connect(String URL,WSMethod method,StringContent body)
{
try
{
HttpClient client = new HttpClient
{
Timeout = TimeSpan.FromSeconds(Variables.SERVERWAITINGTIME)
};
if (await controller.getIsInternetAccessAvailable())
{
if (Variables.CURRENTUSER != null)
{
var authData = String.Format("{0}:{1}:{2}", Variables.CURRENTUSER.Login, Variables.CURRENTUSER.Mdp, Variables.TOKEN);
var authHeaderValue = Convert.ToBase64String(Encoding.UTF8.GetBytes(authData));
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", authHeaderValue);
}
HttpResponseMessage response = null;
if (method == WSMethod.PUT)
response = await client.PutAsync(URL, body);
else if (method == WSMethod.POST)
response = await client.PostAsync(URL, body);
else
response = await client.GetAsync(URL);
....
}
我知道PUT和POST之间的主要区别是幂等性,但我想在实际的层面上理解它。 例如,假设我必须处理用户更改用户名的请求: 有哪些不同之处: 而且 你会用哪一个?
使用dropwizard框架实现了Resful应用程序。我使用dropwizard-auth-jwt对maven包进行身份验证: com.github.toastshaman dropwizard-auth-jwt 1.0.2-0 为了为资源添加身份验证,我实现了sampleAuthenticator,它是使用主体类进行身份验证检查的已实现的身份验证器类。 当MyUser实现主体时: 使用这种配置
HTTP协议中的PUT、POST和PATCH方法有什么区别?
我对Java服务器端编程还不熟悉,我的问题基本上是使用Servlets(低级别,不使用spring mvc等)开始一个起点,然后从node开始构建。js后台,其中路由定义将以函数(,等),函数将在http方法之一(GET、post、PUT、DELETE)的参数中接收和。 如果有人可以帮助我,从一个servlet类中的路由(比如说)开始定义方法,这个servlet类映射到http方法,同时在其参数中
我想提出我的问题的一种方式是:如果我使用put来进行一个非幂等调用,然后使用POST来这样做,会有什么问题呢?
本文向大家介绍HTTP提交方式之PUT详细介绍及POST和PUT的区别,包括了HTTP提交方式之PUT详细介绍及POST和PUT的区别的使用技巧和注意事项,需要的朋友参考一下 Http定义了与 服务器的交互方法,其中除了一般我们用的最多的GET,POST 其实还有PUT和DELETE 根据RFC2616标准(现行的HTTP/1.1)其实还有OPTIONS,GET,HEAD,POST,PUT,D