我正在使用apache-components在本地托管我自己的服务器。我尝试向其发送GET请求,并且收到的响应是预期的。客户端代码的输出为:
{
// dbCategory.Id = Guid.NewGuid();
// CategoryId = model.CategoryId,
dbCategory.VariantName = model.VariantName;
dbCategory.IsExists = model.IsExists != null;
dbCategory.CreatedDate = DateTime.Now;
dbCategory.CreatedBy = model.CreatedBy;
dbCategory.UpdatedDate = DateTime.Now;
dbCategory.UpdatedBy = model.UpdatedBy;
};
dbcontext.CarCategories.Add(dbCategory);
dbcontext.SaveChanges();
}
}
catch (System.Data.Entity.Validation.DbEntityValidationException dbEx)
{
Exception raise = dbEx;
foreach (var validationErrors in dbEx.EntityValidationErrors)
{
foreach (var validationError in validationErrors.ValidationErrors)
{
string message = string.Format("{0}:{1}",
validationErrors.Entry.Entity.ToString(),
validationError.ErrorMessage);
// raise a new exception nesting
// the current instance as InnerException
raise = new InvalidOperationException(message, raise);
}
}
throw raise;
}
return Id;
}
但是,在服务器上,对于我的每个请求,我仍然在服务器日志中看到以下错误:
Sending 'GET' request to URL : http://localhost:65432/test
Response Code : 200
This is the response
我知道服务器实现是正确的,因为当我使用浏览器访问 http://localhost:65432/test 时,错误未出现在服务器日志中。
这是客户端调用GET的代码(从https://www.mkyong.com/java/how-to-send-http-request-getpost-in-java/复制)。
java.io.IOException: An existing connection was forcibly closed by the remote host
以防万一您需要它。这是我的服务器处理程序代码
String url = String.format("%s://%s%s", "http", "localhost:65432", "/test");
URL obj = new URL(url);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
// optional default is GET
con.setRequestMethod("GET");
con.setRequestProperty("User-Agent", "Mozilla/5.0");
int responseCode = con.getResponseCode();
System.out.println("\nSending 'GET' request to URL : " + url);
System.out.println("Response Code : " + responseCode);
BufferedReader in = new BufferedReader(
new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
//print result
System.out.println(response.toString());
如何正确关闭客户端代码中的连接,以使错误不会出现在服务器日志中?