标签: httpresponse

在 Response.End() 之后从代码后面调用 JavaScript 函数

在我的.aspx页面中,我需要div在下载附件后使用 JavaScript 隐藏它。我使用下面的代码来完成它。但自从Response进入现场后,他们HideDiv()就没有被解雇。

我什至尝试将 div 设置为服务器控件并将可见性设置为false. 我还尝试将ClientScript零件放在块之后Response.End();和块内finally。

    try{

    ClientScript.RegisterStartupScript(this.GetType(), "Hide", "HideDiv()", true);
    Response.ContentType = "application/vnd.ms-excel";
    Response.AppendHeader("Content-Disposition", "attachment; filename=" + filename + "");
    Response.Write(tw.ToString());
    Response.End();
}
catch (System.Threading.ThreadAbortException)
        {

        }
        catch (Exception ex)
        {
            //log error
        }
        finally
        {

        }
Run Code Online (Sandbox Code Playgroud)

有什么想法可以让 JavaScript 工作吗?

asp.net httpresponse c#-4.0

5
推荐指数
1
解决办法
9152
查看次数

我应该在 .Net Web Api 2 中返回状态代码还是抛出异常

我曾见过这样的例子这

public IHttpActionResult GetProduct(int id)
{
    Product item = repository.Get(id);
    if (item == null)
    {
        throw new HttpResponseException(HttpStatusCode.NotFound);
    }
    return Ok(item);
}
Run Code Online (Sandbox Code Playgroud)

但我也想象这是一个选择

public IHttpActionResult GetProduct(int id)
{
    Product item = repository.Get(id);
    if (item == null)
    {
        return NotFound();
    }
    return Ok(item);
}
Run Code Online (Sandbox Code Playgroud)

抛出异常或简单地返回 NotFound(IHttpActionResult 实例)是否有优势?

我知道在响应/请求管道中有一些阶段可以处理这些结果中的任何一个,就像第一个例子一样

public class NotFoundExceptionFilterAttribute : ExceptionFilterAttribute 
{
    public override void OnException(HttpActionExecutedContext context)
    {
        if (context.Exception is NotFoundException)
        {
            // Do some custom stuff here ...
            context.Response = new HttpResponseMessage(HttpStatusCode.NotFound);
        }
    }
}

... …
Run Code Online (Sandbox Code Playgroud)

c# asp.net exception-handling httpresponse asp.net-web-api2

5
推荐指数
1
解决办法
3593
查看次数

如何使用 PSR-7 响应?

我的应用程序中的大多数响应要么是视图,要么是 JSON。我不知道如何将它们放入ResponseInterface在PSR-7中实现的对象中。

这是我目前所做的:

// Views
header('Content-Type: text/html; charset=utf-8');
header('Content-Language: en-CA');
echo $twig->render('foo.html.twig', array(
    'param' => 'value'
    /* ... */
));

// JSON
header('Content-Type: application/json; charset=utf-8');
echo json_encode($foo);
Run Code Online (Sandbox Code Playgroud)

这是我试图用 PSR-7 做的事情:

// Views
$response = new Http\Response(200, array(
    'Content-Type' => 'text/html; charset=utf-8',
    'Content-Language' => 'en-CA'
));

// what to do here to put the Twig output in the response??

foreach ($response->getHeaders() as $k => $values) {
    foreach ($values as $v) {
        header(sprintf('%s: %s', $k, $v), false);
    } …
Run Code Online (Sandbox Code Playgroud)

php stream httpresponse php-stream-wrappers psr-7

5
推荐指数
1
解决办法
2445
查看次数

Django:在 HttpResponse 之后重定向

我正在视图中生成报告下载并在处理 POST 数据后开始下载。这意味着用户发送表单并开始下载:

视图.py

def export(request):
    if request.method == 'POST' and 'export_treat' in request.POST: 
        form1 = TransExport(request.POST, instance= obj)
        if form1.is_valid():
            ... 
            ...
            response=HttpResponse(ds.xls,content_type="application/xls")
            response['Content-Disposition'] = 'attachment; filename="Report_Behandlungen.xls"'
            return response
Run Code Online (Sandbox Code Playgroud)

我需要的是下载(或重定向)后的页面刷新。我怎样才能做到这一点?

django redirect refresh httpresponse

5
推荐指数
1
解决办法
1328
查看次数

从 Java 中的 HTTP 响应解析 JSON

嗨,我正在使用客户端 Http (apache) 和 json-simple。

我想访问json响应的属性,然后使用它们。

知道如何做到这一点吗?我读了一篇文章,但我并没有像它那样工作。

这是我的回答 json:

{"Name":"myname","Lastname":"mylastname","Age":19}
Run Code Online (Sandbox Code Playgroud)

这是我的代码java:

DefaultHttpClient httpClient = new DefaultHttpClient();

HttpGet getRequest = new HttpGet(
    "http://localhost:8000/responsejava");
getRequest.addHeader("accept", "application/json");

HttpResponse response = httpClient.execute(getRequest);

if (response.getStatusLine().getStatusCode() != 200) {
    throw new RuntimeException("Failed : HTTP error code : "
             + response.getStatusLine().getStatusCode());
}

BufferedReader br = new BufferedReader(
    new InputStreamReader( 
        (response.getEntity().getContent())
    )
);

StringBuilder content = new StringBuilder();
String line;
while (null != (line = br.readLine())) {
    content.append(line);
}

Object obj=JSONValue.parse(content.toString());
JSONObject finalResult=(JSONObject)obj;
System.out.println(finalResult);

httpClient.getConnectionManager().shutdown();
Run Code Online (Sandbox Code Playgroud)

我打印了 null,我做错了什么?

java json http httpresponse json-simple

5
推荐指数
1
解决办法
3万
查看次数

Java 中的 HTTP 触发并忘记

我们正在实施我们自己的分析,因为我们公开了一个需要调用的 Web 服务,该服务将捕获我们数据库中的数据。

问题是,由于这是分析,我们将进行大量调用(例如每个页面加载、每个 js 之后调用、CSS 加载等),因此会有很多这样的调用。所以我不希望服务器加载大量请求,以便更精确地等待响应。因为我们得到的回应对我们几乎没有任何用处。

那么有什么方法可以只触发 Web 服务请求而忘记我已经触发了它?

我知道每个 HTTP 请求也会有响应。

因此,让我想到的一件事是,如果我们将请求超时设置为零秒会怎样?但我不确定这是否是正确的做法。

请为我提供更多建议

java analytics httpresponse httpclient httprequest

5
推荐指数
1
解决办法
9720
查看次数

可以在浏览器中查看哪些文件类型(内联,无需插件)

我只想知道可以在浏览器(如 Chrome、Firefox)中查看哪些文件类型(如 .png、.pdf、.txt)。因为有时当我点击文件链接时,它会自动下载而不是在浏览器中显示。为什么会发生这种情况?下面是我的代码。任何帮助将不胜感激。

<script>

function openPDF(url){
   var w=window.open(url, '_blank');
   w.focus();
}

</script>
<a href="burger1.jpg" target='_blank'>link</a>
<div onclick="openPDF('1.pdf');">PDF 1</div>
Run Code Online (Sandbox Code Playgroud)

html browser download httpresponse http-headers

5
推荐指数
2
解决办法
4596
查看次数

如何从 Selenium webdriver Firefox 中的 `driver.page_source` 获取 HTTP 请求的原始 JSON 响应

如果我浏览到https://httpbin.org/headers我希望得到以下 JSON 响应:

{
  "headers": {
    "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8", 
    "Accept-Encoding": "gzip, deflate, br", 
    "Accept-Language": "en-US,en;q=0.5", 
    "Connection": "close", 
    "Host": "httpbin.org", 
    "Upgrade-Insecure-Requests": "1", 
    "User-Agent": "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:64.0) Gecko/20100101 Firefox/64.0"
  }
}
Run Code Online (Sandbox Code Playgroud)

但是,如果我使用硒

from selenium import webdriver
from selenium.webdriver.firefox.options import Options

options = Options()
options.headless = True
driver = webdriver.Firefox(options=options)

url = 'https://httpbin.org/headers'
driver.get(url)
print(driver.page_source)
driver.close()
Run Code Online (Sandbox Code Playgroud)

我得到

<html platform="linux" class="theme-light" dir="ltr"><head><meta http-equiv="Content-Security-Policy" content="default-src 'none' ; script-src resource:; "><link rel="stylesheet" type="text/css" href="resource://devtools-client-jsonview/css/main.css"><script type="text/javascript" charset="utf-8" async="" data-requirecontext="_" data-requiremodule="viewer-config" src="resource://devtools-client-jsonview/viewer-config.js"></script><script type="text/javascript" charset="utf-8" …
Run Code Online (Sandbox Code Playgroud)

python selenium json httpresponse selenium-webdriver

5
推荐指数
3
解决办法
1万
查看次数

Axios 获取请求成功但数据属性为空?

我有一个简单的反应组件:

import React from "react"
import axios from "axios"

export default () => {
  const axios = require("axios")

  axios.get("http://localhost:8000/user").then(function(res) {
    // handle success
    console.log(res)
  })

  return (
    <div>
        Hello
    </div>
  )
}
Run Code Online (Sandbox Code Playgroud)

我确实得到了一些东西,状态代码为 200,并且它执行了 console.log,但数据字段为空。这只是data: ""; 我发出的 get 请求是针对我自己的 nodejs 服务器。我应该找回当前登录的用户。这在我直接访问 URL 时有效 ( localhost:8000/user)。看起来像这样

"displayName": "thomas",
"id": "google-oauth2|197913719739127391723",
"user_id": "google-oauth2|197913719739127391723",
"name": {
"givenName": "thomas3000"
},
"emails": [
{
"value": "thomas3k@tmail.com"
}
],
"picture": "https://akfaf/afaf/photo.jpg",
"locale": "en",
"nickname": "thomas3000",
"_json": {
"sub": "google-oauth2|197913719739127391723",
"given_name": "thomas3k",
"nickname": "tom", …
Run Code Online (Sandbox Code Playgroud)

javascript http httpresponse node.js axios

5
推荐指数
0
解决办法
3418
查看次数

Scala 和 Akka HTTP:请求中的请求和线程问题

我刚开始学习 Scala、Akka Streams 和 Akka HTTP,所以如果问题太基本,请提前道歉。

我想在 HTTP 请求中执行一个 HTTP 请求,就像在下面的一段代码中一样:

  implicit val system = ActorSystem("ActorSystem")
  implicit val materializer = ActorMaterializer
  import system.dispatcher

  val requestHandler: Flow[HttpRequest, HttpResponse, _] = Flow[HttpRequest].map {
    case HttpRequest(HttpMethods.GET, Uri.Path("/api"), _, _, _) =>
      val responseFuture = Http().singleRequest(HttpRequest(uri = "http://www.google.com"))
      responseFuture.onComplete {
        case Success(response) =>
          response.discardEntityBytes()
          println(s"The request was successful")
        case Failure(ex) =>
          println(s"The request failed with: $ex")
      }
      //Await.result(responseFuture, 10 seconds)
      println("Reached HttpResponse")
      HttpResponse(
        StatusCodes.OK
      )
  }

  Http().bindAndHandle(requestHandler, "localhost", 8080)  
Run Code Online (Sandbox Code Playgroud)

但在上述情况下,结果如下所示,这意味着Reached HttpResponse在完成请求之前首先到达:

Reached …
Run Code Online (Sandbox Code Playgroud)

scala httpresponse httprequest akka-stream akka-http

5
推荐指数
1
解决办法
252
查看次数