是否可以进行这样的HTTP响应,浏览器会忽略它并继续显示以前显示的页面?
我的意思是以下场景:
a)用户点击某些内容
b)一些POST进入服务器(或GET,但让我们坚持使用POST更有趣)
c)服务器决定由于某种原因它不想在此时发送回复
d)服务器发送特制的响应
e)浏览器不显示错误页面,不显示空白页面,不重定向任何地方,不重新加载 - 只是继续显示它显示的内容,就像用户从未提交过表单一样
PS当然它可以用AJAX包装,但我问是否可以使用简单的非JavaScript解决方案
我正在使用另一个Stack Overflow问题的技术将CSV文件写入Response输出以供用户打开/保存.该文件在记事本中看起来不错,但是当我在Excel中打开它时,重音字符是垃圾.我认为这与字符编码有关,所以我尝试手动将其设置为UTF-8(默认为StreamWriter).这是代码:
// This fills a list to enumerate - each record is one CSV line
List<FullRegistrationInfo> fullUsers = GetFullUserRegistrations();
context.Response.Clear();
context.Response.AddHeader("content-disposition",
"attachment; filename=registros.csv");
context.Response.ContentType = "text/csv";
context.Response.Charset = "utf-8";
using (StreamWriter writer = new StreamWriter(context.Response.OutputStream))
{
for (int i = 0; i < fullUsers.Count(); i++)
{
// Get the record to process
FullRegistrationInfo record = fullUsers[i];
// If it's the first record then write header
if (i == 0)
writer.WriteLine(Encoding.UTF8.GetString(
Encoding.UTF8.GetPreamble()) +
"User, …Run Code Online (Sandbox Code Playgroud) 我的服务器正在向请求发送带有正文的示例响应标头:
static char* not_found_response_template =
"HTTP/1.1 404 Not Found\n"
"Content-type: text/html\n"
"\n"
"<html>\n"
" <body>\n"
" <h1>Not Found</h1>\n"
" <p>The requested URL was not found on this server.</p>\n"
" </body>\n"
"</html>\n";
len = strlen(not_found_response_template);
send(newSct, not_found_response_template, len, 0);
Run Code Online (Sandbox Code Playgroud)
它正确发送它,但firefox继续加载,直到我取消传输.
firefox插件HttpRequestHelper显示了这个:
获取localhost:6666
- 响应 - 404未找到内容类型:text/html
为什么内容没有加载?
我正在尝试实现以下内容:
简单的控制器和动作.Action应根据请求返回2种类型的响应:
HTML in case of ordinary request (text\html),
JSON in case of ajax request (application\json)
Run Code Online (Sandbox Code Playgroud)
我已经设法通过控制器的插件来做到这一点,但这需要写
return $this->myCallBackFunction($data)
Run Code Online (Sandbox Code Playgroud)
在每个行动中.如果我不想对整个控制器做这个怎么办?试图找出如何通过事件监听器实现它,但无法成功.
任何提示或链接到一些文章将不胜感激!
在我的Global.asax中,我定义了Application_error方法:
protected void Application_Error(object sender, EventArgs e)
{
// Code that runs when an unhandled error occurs
// Get the exception object.
var exc = Server.GetLastError();
//logics
Response.Redirect(String.Format("~/ControllerName/MethodName?errorType={0}", errorAsInteger));
}
Run Code Online (Sandbox Code Playgroud)
并且exc变量确实保留了最后一个错误但是在响应方法(MethodName)中从响应重定向后,它Server.GetLastError()是null.我怎样才能保留它或将其传递给Response.Redirect(String.Format("~/ControllerName/MethodName?errorType={0}"我所以我可以将我的方法体中的异常作为对象?
model-view-controller asp.net-mvc response application-error global-asax
我有一个Windows服务,它将文件上传到正在处理它们的其他网站.问题是,对于小文件,它工作正常并且从那里获得响应,但是对于大文件(大约6分钟处理),它将永远处于等待模式.
这是外部网站发布方法代码的一部分:
try
{
...
LogResults();
return string.Empty;
}
catch (Exception e)
{
return e.Message;
}
Run Code Online (Sandbox Code Playgroud)
问题是我甚至可以看到大型文件的日志,所以这意味着网站总是返回值,但对于大型文件,我的Windows服务不会等待它们.
这是来自Windows服务的代码
var valuesp = new NameValueCollection
{
{ "AccountId", datafeed.AccountId }
};
byte[] resultp = UploadHelper.UploadFiles(url, uploadFiles, valuesp);
response = Encoding.Default.GetString(resultp);
Run Code Online (Sandbox Code Playgroud)
UploadFiles method返回小文件的值,但永远等待大文件.
这是UploadFiles的完整代码
public static byte[] UploadFiles(string address, IEnumerable<UploadFile> files, NameValueCollection values)
{
var request = WebRequest.Create(address);
request.Timeout = System.Threading.Timeout.Infinite; //3600000; // 60 minutes
request.Method = "POST";
var boundary = "---------------------------" +
DateTime.Now.Ticks.ToString("x", NumberFormatInfo.InvariantInfo);
request.ContentType = "multipart/form-data; boundary=" + boundary;
boundary = …Run Code Online (Sandbox Code Playgroud) 我正在使用Laravel 4.2.使用类的make方法Response我得到未定义的方法错误.
Route::get('/', function()
{
$contents = "Hello";
$response = Response::make($contents, 200);
return $response;
});
Run Code Online (Sandbox Code Playgroud)
这是错误
我有一个休息api,我想在Swagger中记录.在所有请求中,API都可以使用401响应.
因此,不是为每条路径一次又一次地再次定义401(不是那么干).我想定义所有路径都可以返回401.
这可能吗?
我有一个TeamResponse类用于形成JSON响应.它有一些参数,其中一个可以是可选的:
public TeamResponse(Team team, List<TeamSkillTemplateResponse> teamSkillTemplateResponses) {
this.id = team.getId();
this.name = team.getName();
this.department = new DepartmentResponse(team.getDepartment());
this.division = new DivisionResponse(team.getDepartment().getDivision());
this.valueStream = team.getValueStream().map(ValueStreamResponse::new).orElseGet(null);
this.skillTemplate = teamSkillTemplateResponses;
}
Run Code Online (Sandbox Code Playgroud)
我实际上有两个问题:1.如果值不存在,在响应中返回null是否合适?2.以这种方式返回null是否合适?(方法getTeam()返回Optional)
谢谢.
我想将CSRF中间件与Django中的API视图一起使用。这是我想与CSRF一起使用的演示视图,我很困惑如何在此处集成CSRF。
def login(request):
try:
if len(DemoTable.objects.filter(phone=int(request.POST['user'])).filter(password=sha1Engine(request.POST['password'])))==1:
print(DemoTable.objects.filter(phone=int(request.POST['user'])).filter(password=sha1Engine(request.POST['password'])))
return JsonResponse({'exit':'0','msg':'Success'})
return JsonResponse({'exit':'2','msg':'User Invalid'})
except Exception as e:
return JsonResponse({'exit':'10','msg':'Unknown Error Occured'})
Run Code Online (Sandbox Code Playgroud)
任何帮助或建议,将不胜感激。谢谢。