我试图确定为什么网络服务器响应最初在处理过程中抛出异常;然后客户端返回200 OK。详情如下所示:
请求从 Web 应用程序发送到 Web 服务器,如果发生错误,则会捕获异常并返回相关代码和/或消息,如下所示:
public void dispatchRequest(HttpServletRequest req, HttpServletResponse
res)
{
if (method.equalsIgnoreCase("get")) {
doGet(req, res);
} else {
res.sendError(HttpServletResponse.SC_NOT_IMPLEMENTED);
return;
}
}
void doGet(HttpServletRequest request, HttpServletResponse response)
throws
IOException,
HTTPServerException {
handleGetClient(request, response);
}
@SuppressWarnings("unchecked")
private void handleGetClient(HttpServletRequest request,
HttpServletResponse
response)
throws IOException, HTTPServerException {
...
} catch (IOException e) {
logger("I/O Error during playback with parameters (additional
parameters logged) {0}: {1}",traceParams,e.toString());
logger(Level.FINER, "I/O Error during playback with parameters {0}:
{1}", parameters, e.getMessage());
logger(Level.FINER, "I/O Error …Run Code Online (Sandbox Code Playgroud)我的应用程序使用 Vapor 4.3 并具有发送 HTML 片段作为响应的简单路由:
import Vapor
func routes(_ app: Application) throws {
app.get("hello") { _ -> String in
"<html><body>Hello, world!</body></html>"
}
}
Run Code Online (Sandbox Code Playgroud)
不幸的是,此响应没有Content-Type设置正确的 HTTP 标头,因此当在浏览器中打开此路由时,它不会呈现为正确的 HTML。在此响应上设置标头的最佳方法是什么Content-Type?
我正在从输出一些 json 内容的 API 检索数据。但是,当我尝试使用以下代码将数据存储到简单的文本文件中时:
import urllib3
import json
http = urllib3.PoolManager()
url = 'http://my/endpoint/url'
myheaders = {'Content-Type':'application/json'}
mydata = {'username':'***','password':'***'}
response = http.request('POST', url, body=json.dumps(mydata).encode('UTF-8'), headers=myheaders)
print(response.status_code)
data = response.json()
with open('data.json', 'w') as f:
json.dump(data, f)
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
AttributeError: 'HTTPResponse' object has no attribute 'json'
Run Code Online (Sandbox Code Playgroud)
因此,我还尝试使用 response.text 和以下代码:
file = open('data.json', 'w')
file.write(response.text)
file.close()
Run Code Online (Sandbox Code Playgroud)
但我也收到此错误:
AttributeError: 'HTTPResponse' object has no attribute 'text'
Run Code Online (Sandbox Code Playgroud)
为什么我不能将我的回复存储到简单的文本文件中?
我在 odoo 14 中创建了一条 Json 类型的路线。
@http.route('/test', auth='public', methods=['POST'], type="json", csrf=False)
def recieve_data(self, **kw):
headers = request.httprequest.headers
args = request.httprequest.args
data = request.jsonrequest
Run Code Online (Sandbox Code Playgroud)
因此,当我使用此路由收到请求时,一切都很好,但假设我想返回不同的状态代码(如 401),我无法使用类型为 json 的路由来执行此操作。我也尝试过以下方法,但此方法的问题是它会停止所有 odoo 未来的请求。
from odoo.http import request, Response
@http.route('/test', auth='public', methods=['POST'], type="json", csrf=False)
def recieve_data(self, **kw):
headers = request.httprequest.headers
args = request.httprequest.args
data = request.jsonrequest
Response.status = "401 unauthorized"
return {'error' : 'You are not allowed to access the resource'}
Run Code Online (Sandbox Code Playgroud)
因此,在上面的示例中,如果我将响应状态代码设置为 401 ,则所有其他请求(即使它们发送到不同的路由)也将被停止,并且其状态代码更改为 401 。我在odoo14和odoo13中都检查过这个问题。
给出表达式:
responseBodyJson = await responseMsg.Content.ReadAsStringAsync();
Run Code Online (Sandbox Code Playgroud)
知道responseMsg是一个HttpResponseMessage对象并且不为 null,那么Content( HttpContentobject) 是否可以为 null,换句话说,这个表达式可以抛出一个 吗NullReferenceException?
在MSDN文档在这方面非常无益的,因为我遇到了这个方法,是不完全知道为什么我会想这样做或做不到这一点.
注意:任何人都应该回答"好吧,它将文件读入内存",没有进一步的解释将被低估.
是否有一种简单的方法来解析HTTP响应字符串,如下所示:
"HTTP/1.1 200 OK\r\nContent-Length: 1433\r\nContent-Type: text/html\r\nContent-Location: http://server/iisstart.htm\r\nLast-Modified: Fri, 21 Feb 2003 23:48:30 GMT\r\nAccept-Ranges: bytes\r\nETag: \"09b60bc3dac21:1ca9\"\r\nServer: Microsoft-IIS/6.0\r\nX-Po"
Run Code Online (Sandbox Code Playgroud)
我想获得状态代码.我不一定需要将其转换为HttpResponse对象,但这只是解析状态代码是可以接受的.我可以将其解析为HttpStatusCode枚举吗?
我正在使用基于套接字的方法,并且无法改变我获得响应的方式.我只会使用此字符串.
我不希望网站的apache返回任何HTTP代码404,
原因是认证委员会将扫描所有网站,
许多javascript链接和一些事件将导致404代码,
事实上,这些链接是有效但但是检查员会误判错误.
所以,我想阻止apache返回HTTP代码404,
如何配置apache 返回HTTP代码200而不是404并显示我的定义错误页面?
我正在尝试使用spring-mvc,spring-boot和spring security构建一个小型Web应用程序.只使用一个控制器,其中一个服务端点是让用户下载Web应用程序生成的docx文件.我的逻辑代码运行良好,问题是当我想将Headers添加到HttpServletResponse时,addHeader()和setHeader()不起作用,我只想指定下载文件的名称.我打印了一些日志并且不知道为什么这不起作用.
这是我的控制器的部分代码:
@Controller
public class ImportExportController {
private final static Logger LOGGER = LoggerFactory.getLogger(ImportExportController.class);
@Autowired
private WordProcessor wordProcessor;
@RequestMapping("/export")
public void export(@RequestParam(value = "domainName", required = true) String domainName,
@RequestParam(value = "projectName", required = true) String projectName,
@RequestParam(value = "testFolderId", required = true) int testFolderId,
HttpServletRequest request, HttpServletResponse response) {
String exportedFileName = "exportedTC_" + domainName + "_" + projectName + "_"
+ Integer.toString(testFolderId) + ".docx";
try {
extendExpiredALMSession();
SaveToZipFile saver = wordProcessor.ExportToWord(domainName, projectName,
Integer.toString(testFolderId));
saver.save(response.getOutputStream());
response.setContentType("application/vnd.openxmlformats-officedocument.wordprocessingml.document");
LOGGER.info("exportedFileName: " …Run Code Online (Sandbox Code Playgroud) 该RFC说,content-length头是可选的("..Applications应使用此字段...").
从我可以收集的内容中,如果不包括在内,那么客户端将不知道预期有多少数据,因此在下载正文时(即顶部栏而不是底部)将无法显示确定的进度条.

省略此标题是否还有其他副作用或错误?
httpresponse ×10
http ×4
c# ×3
python-3.x ×2
servlets ×2
.net-core ×1
apache ×1
asp.net ×1
content-type ×1
error-code ×1
http-headers ×1
httprequest ×1
java ×1
json-rpc ×1
odoo ×1
python ×1
response ×1
sockets ×1
spring ×1
spring-mvc ×1
swift ×1
vapor ×1
vb.net ×1