我正在寻找一个库,用Arduino解析Web HTTP响应,例如,从内容中分离HTTP Headers.
阅读各种草图,解析数据流的最先进工具是TextFinder.
您是否了解任何其他可以帮助解析HTTP响应的库?
我已经看到一些非常难看的代码来自人们编写自己的方法将HttpResponse转换为字符串以便稍后使用,看起来像这样:
httppost.setEntity(new UrlEncodedFormEntity(valuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
InputStream is = entity.getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(is,"UTF8"),8);
StringBuilder sb = new StringBuilder();
sb.append(reader.readLine() + "\n");
String line="0";
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
String result = sb.toString();
Run Code Online (Sandbox Code Playgroud)
这不仅是一个混乱,但它真的很难看,而且我常常无法分辨代码发生了什么,因为这个混乱在它之前.有没有更好的方法来做到这一点?
我有大约40个具有类似基本响应结构的API,如下所示:
{
"lastAccessed": "2015-30-08:14:21:45T",
"createdOn": "2015-30-07:09:04:10T",
"lastModified": "2015-30-08:14:21:45T",
"isReadOnly": "false",
"usersAllowed" : ["Tim", "Matt", "Christine"];
"noOfEntries": 1,
"object": [
"ObjectA": {
//here object A has its own model
}
]
}
Run Code Online (Sandbox Code Playgroud)
所以我有一个基类响应类,它采用类型T的泛型,如下所示:
public class Response<T> {
@ApiModelProperty(value="Last time accessed")
private String lastAccessed;
@ApiModelProperty(value="Time when Created ")
private String createdOn;
private String lastModified;
@ApiModelProperty(value="Created on")
private boolean isReadOnly;
@ApiModelProperty(value="Users that has access to the object.")
private List<String> usersAllowed;
private int noOfEntries;
private T object;
//getters and setters
}
Run Code Online (Sandbox Code Playgroud)
因此对于API A,它返回具有自己字段的Object类型,我将返回Response作为控制器中的API响应:
public …Run Code Online (Sandbox Code Playgroud) 我有功课,我需要以某种方式比较两个HTTP响应.我在C上写它,我使用libcurl使事情变得更容易.我正在调用使用libcurl来执行HTTP请求和来自另一个函数的响应的函数,我想将HTTP响应作为一个返回char *.这是我到目前为止的代码(它崩溃):
#include <stdio.h>
#include <curl/curl.h>
#include <string.h>
size_t write_data(void *ptr, size_t size, size_t nmemb, void *stream) {
size_t written;
written = fwrite(ptr, size, nmemb, stream);
return written;
}
char *handle_url(void) {
CURL *curl;
char *fp;
CURLcode res;
char *url = "http://www.yahoo.com";
curl = curl_easy_init();
if (curl) {
curl_easy_setopt(curl, CURLOPT_URL, url);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);
res = curl_easy_perform(curl);
if(res != CURLE_OK)
fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res));
curl_easy_cleanup(curl);
//printf("\n%s", fp);
}
return fp;
}
Run Code Online (Sandbox Code Playgroud)
这个解决方案C libcurl得到输出到一个字符串的 …
我正在寻找一种方法来获取抛出的VolleyError的响应代码.我的ErrorListener看起来像这样:
Response.ErrorListener errorListener = new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
error.printStackTrace();
//Get response code here
VolleyLog.e("Error: ", error.toString());
VolleyLog.e("Error: ", error.getLocalizedMessage());
}
};
Run Code Online (Sandbox Code Playgroud)
我发送给我的VolleyLog的2行打印如下:
03-12 10:57:56.932: E/Volley(7147): [1] 1.onErrorResponse: Error:
03-12 10:57:56.932: E/Volley(7147): [1] 1.onErrorResponse: Error:
Run Code Online (Sandbox Code Playgroud)
Volley确实知道回来的原因,因为我也能在VolleyLog中看到以下内容:
03-12 10:57:56.692: E/Volley(7147): [41854] BasicNetwork.performRequest: Unexpected response code 403 for https://*******/Employee/authenticate
03-12 10:57:56.897: E/Volley(7147): [41854] BasicNetwork.performRequest: Unexpected response code 403 for https://*******/Employee/authenticate
03-12 10:57:56.902: W/System.err(7147): com.android.volley.AuthFailureError
03-12 10:57:56.902: W/System.err(7147): at com.android.volley.toolbox.BasicNetwork.performRequest(BasicNetwork.java:143)
03-12 10:57:56.902: W/System.err(7147): at com.android.volley.NetworkDispatcher.run(NetworkDispatcher.java:105)
Run Code Online (Sandbox Code Playgroud)
所以基本上,我想要的是获得响应代码"403"(禁止).
这个问题很简单,但我找不到任何数据.当我使用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) if(condition true){
request.sendRedirect("//Some URL");}
// Some other code
Run Code Online (Sandbox Code Playgroud)
现在我的问题是当我们重定向时,其他一些代码会执行还是不执行?如果执行时,在sendRedirect之前或sendRedirect之后?
我有一个以下的jQuery帖子请求:
$.post("ajax.php", data).done(
function(response){
// do something when response is ok
}
).fail(
function(response){
$("#error").html(response);
}
);
Run Code Online (Sandbox Code Playgroud)
在我的ajax.php文件出现问题时,我返回HTTP响应代码400,并带有一条消息:
header("HTTP/1.0 400 Error");
echo "Some error message";
exit();
Run Code Online (Sandbox Code Playgroud)
当我在浏览器中检查错误调用的响应时,我可以看到状态代码Status Code:400 Bad Request和我在错误消息中传递的响应文本.但是jQuery的.fail回调并没有显示我的response.
如何访问失败/错误响应文本?
编辑
我测试了我的代码,并且.fail正在触发回调,我只是无法显示响应消息.
我们正在实现基于 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) …