这个问题很简单,但我找不到任何数据.当我使用reportlab生成pdf,将httpresponse作为文件传递时,配置为显示文件的浏览器会正确显示pdf.但是,选项卡的标题仍为"(匿名)127.0.0.1/whatnot",这对用户来说有点难看.
由于大多数网站能够以某种方式显示适当的标题,我认为这是可行的...是否有某种标题参数,我可以传递给PDF?或者响应的一些标题?这是我的代码:
def render_pdf_report(self, context, file_name):
response = HttpResponse(content_type='application/pdf')
response['Content-Disposition'] = 'filename="{}"'.format(file_name)
document = BaseDocTemplate(response, **self.get_create_document_kwargs())
# pdf generation code
document.build(story)
return response
Run Code Online (Sandbox Code Playgroud) 我们正在实现基于 REST 的 Web 服务,并且对某些用例有一些疑问。
考虑有一个唯一的帐户,其中包含一些信息(例如添加到购物车信息)
应使用什么 HTTP 状态代码?
以下是指具有如下依赖项的 .NET Core 应用程序...
Microsoft.NETCore.App
Microsoft.AspNet.WepApi.Client (5.2.7)
Run Code Online (Sandbox Code Playgroud)
Microsoft.com 上有 2017 年 11 月的文档Call a Web API From a .NET Client (C#)。
文档中包含 HTTP GET 的客户端调用。
static HttpClient client = new HttpClient();
static async Task<Product> GetProductAsync(string path)
{
Product product = null;
HttpResponseMessage response = await client.GetAsync(path);
if (response.IsSuccessStatusCode)
{
product = await response.Content.ReadAsAsync<Product>();
}
return product;
}
Run Code Online (Sandbox Code Playgroud)
该值response.Content指的是一个HttpContent对象。截至 2020 年 7 月,HttpContent没有带有签名的实例方法ReadAsAsync<T>(),至少根据以下文档。然而,这个实例方法是有效的。
没有带有签名的实例方法的参考链接ReadAsAsync<T>()...
https://learn.microsoft.com/en-us/dotnet/api/system.net.http.httpcontent?view=netcore-3.1
有一个HttpContentExtensions.ReadAsAsync<T>(myContent) …
我正在尝试将我从HttpPost获取的XML HttpResponse解析为服务器(last.fm),用于last.fm android应用程序.如果我简单地将其解析为字符串,我可以看到它是一个普通的xml字符串,包含所有需要的信息.但我只是无法解析单个NameValuePairs.这是我的HttpResponse对象:
HttpResponse response = client.execute(post);
HttpEntity r_entity = response.getEntity();
Run Code Online (Sandbox Code Playgroud)
我尝试了两种不同的东西,而不是它们都有效.首先,我试图检索NameValuePairs:
List<NameValuePair> answer = URLEncodedUtils.parse(r_entity);
String name = "empty";
String playcount = "empty";
for (int i = 0; i < answer.size(); i++){
if (answer.get(i).getName().equals("name")){
name = answer.get(i).getValue();
} else if (answer.get(i).getName().equals("playcount")){
playcount = answer.get(i).getValue();
}
}
Run Code Online (Sandbox Code Playgroud)
在此代码之后,name和playcount保持"空".所以我尝试使用XML Parser:
DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
Document answer = db.parse(new DataInputStream(r_entity.getContent()));
NodeList nl = answer.getElementsByTagName("playcount");
String playcount = "empty";
for (int i = 0; i < nl.getLength(); i++) {
Node n = nl.item(i); …Run Code Online (Sandbox Code Playgroud) 我一直在研究一个Android项目,我想要一些API来获取信息.看起来这应该是非常基本的!
这是我的代码的一般要点:
private InputStream retrieveStream2(String url)
{
DefaultHttpClient client = new DefaultHttpClient();
HttpGet getRequest = new HttpGet(url);
System.out.println("getRequest == " + getRequest);
try {
HttpResponse getResponse = client.execute(getRequest);//here is teh problem
final int statusCode = getResponse.getStatusLine().getStatusCode();
if (statusCode != HttpStatus.SC_OK)
{
Log.w(getClass().getSimpleName(),
"Error " + statusCode + " for URL " + url);
return null;
}
HttpEntity getResponseEntity = getResponse.getEntity();
return getResponseEntity.getContent();
}
catch (Exception e)
{
getRequest.abort();
Log.w(getClass().getSimpleName(), "Error for URL, YO " + url, e);
}
return null; …Run Code Online (Sandbox Code Playgroud) 我收到错误"目标主机不能为空,或在参数中设置".
这是我的代码:
String url = "http://maps.google.com/maps/api/directions/json?origin=1600 Pennsylvania Avenue NW, Washington, DC 20500&destination=1029 Vermont Ave NW, Washington, DC 20005&sensor=false";
HttpClient httpclient = new DefaultHttpClient();
String goodURL = convertURL(url);//change weird characters for %etc
HttpPost httppost = new HttpPost(goodURL);
HttpResponse response = httpclient.execute(httppost);
Run Code Online (Sandbox Code Playgroud)
在第5行(上面的最后一行),我的程序抛出异常.这是确切的错误:
java.lang.IllegalArgumentException: Host name may not be null
Run Code Online (Sandbox Code Playgroud)
我在方法convertURL中编码我的字符串...
goodURL = http://maps.google.com/maps/api/directions/json?origin=3%20Cedar%20Ave%2c%20Highland%20Park%2c%20NJ%2008904&destination=604%20Bartholomew%20Road%2c%20Piscataway%2c%20New%20Jersey%2008854&sensor=false
有什么建议?谢谢!
我查询是否System.getProperty("line.separator")和"\n"操作的Android网络相同.我的意思是说.我将从某个服务器获得行分离响应,所以最好使用System.getProperty("line.separator")或"\n"?
在Android中的字符串中使用\n重新提交回车/换行符,但仍然不确定网络操作.
我正在构建一个RESTful API.当客户端在不支持它的资源上使用不受支持的方法(如POST)时,我将返回一个405带有Allow列出允许方法的标头:
Status Code: 405 Method Not Allowed
Allow: GET, HEAD, OPTIONS
Connection: keep-alive
Date: Mon, 08 Apr 2013 00:19:26 GMT
Transfer-Encoding: chunked
Run Code Online (Sandbox Code Playgroud)
是否允许有405响应的正文(提供错误消息)?
从w3c的网站来看,不清楚是否允许一个机构405做出回应.
在REST客户端的HTTP生成器返回HttpResponseDecorator.如何从中获取原始响应(用于记录目的)?
编辑(一些代码可能很方便):
withRest(uri: domainName) {
def response = post(path: 'wsPath', query: [q:'test'])
if (!response.success) {
log.error "API call failed. HTTP status: $response.status"
// I want to log raw response and URL constructed here
}
Run Code Online (Sandbox Code Playgroud) 我正在尝试创建一个简单的Python服务器来测试我的前端.它应该能够处理GET和POST请求.数据应始终采用JSON格式,直到它们转换为HTTP请求/响应.应调用具有相应名称的脚本来处理每个请求.
server.py
#!/usr/bin/env python
from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer
import SocketServer
import json
import urlparse
import subprocess
class S(BaseHTTPRequestHandler):
def _set_headers(self):
self.send_response(200)
self.send_header('Content-type', 'application/json')
self.end_headers()
def do_GET(self):
self._set_headers()
parsed_path = urlparse.urlparse(self.path)
request_id = parsed_path.path
response = subprocess.check_output(["python", request_id])
self.wfile.write(json.dumps(response))
def do_POST(self):
self._set_headers()
parsed_path = urlparse.urlparse(self.path)
request_id = parsed_path.path
response = subprocess.check_output(["python", request_id])
self.wfile.write(json.dumps(response))
def do_HEAD(self):
self._set_headers()
def run(server_class=HTTPServer, handler_class=S, port=8000):
server_address = ('', port)
httpd = server_class(server_address, handler_class)
print 'Starting httpd...'
httpd.serve_forever()
if __name__ == "__main__":
from sys import argv …Run Code Online (Sandbox Code Playgroud) httpresponse ×10
android ×4
http-post ×2
java ×2
python ×2
api ×1
asp.net-core ×1
c# ×1
django ×1
groovy ×1
httpbuilder ×1
httprequest ×1
json ×1
linux ×1
parsing ×1
reportlab ×1
rest ×1
server ×1
web-services ×1
xml ×1