总的来说,我对 REST 和 Web 应用程序还很陌生。我需要实现一个 REST 调用,为系统添加更多资源。有些资源只能通过 XML 创建,例如文件夹。我猜测这将是一个 PUT 请求,并在正文中放置描述资源的 XML 并在服务器上处理请求并创建文件夹。有些资源需要描述(它是什么,它属于谁......等)并提供文件。例如图像。
我有几个问题: 1. 处理 httpServletRequest 的最佳实践是什么?它看起来有点麻烦,我读过有关 HttpClient 的内容,但我不确定。2. 这听起来像是每个 RESTful 服务器都在做的一个非常常见的任务。有没有好的教程/指南?
谢谢
我在调用 API 时遇到一些问题。我需要发送包含两件事的发布数据:一个 ID 和一个 int 数组。我尝试了很多方法,但都导致错误或根本没有以正确的方式发送数据。我找到的所有答案都没有处理我想发送两种不同数据类型的事实。
在另一边我有:
Call<DefaultResponseList> someMethod(@Field("parm1") String parm1, @Field("parm2[]") ArrayList<Integer> parm2);
Run Code Online (Sandbox Code Playgroud)
我试过:
List<int> ints1 = new List<int>();
ids.Add(1);
ids.Add(2);
ids.Add(3);
var values = new List<KeyValuePair<string, object>>
{
new KeyValuePair<string, object>("parm1", "lkdjfowejfd123"),
new KeyValuePair<string, object>("parm2", ints1)
};
var httpClient = new HttpClient(new HttpClientHandler());
HttpResponseMessage response = await httpClient.PostAsync(someUrl, new FormUrlEncodedContent(values));
response.EnsureSuccessStatusCode();
string data = await response.Content.ReadAsStringAsync();
Run Code Online (Sandbox Code Playgroud) 我在配料.model.ts 中声明了一个模型
export class Ingredient {
constructor(private name: string, public amount: number) {}
getName() { return this.name }
}
Run Code Online (Sandbox Code Playgroud)
在配料.service.ts 中,如果我以这种方式获取它们:
httpClient.get<Ingredient>(url).subscribe(
(igredient) => {
console.log(igredient.getName());
});
Run Code Online (Sandbox Code Playgroud)
它在控制台中给出错误,例如“属性 igrient 中没有方法 getName”。
另外,每当我尝试声明属性类型 Category[] 时,它都会失败,但 Array 似乎工作正常。
编辑: 我想提供更多信息。
给定 Igredient 模型和以下 JSON 结构:
{
name: "Apple",
amount: "5",
created_at: "date",
}
Run Code Online (Sandbox Code Playgroud)
Igredient 构造函数甚至没有被调用,因此 GET 有效负载不会被解析。
我正在尝试向我的 API 调用添加一个简单的 HTTP 重新尝试,如下所示
this.http.post(url, {payload})
.pipe(retry(3)).sub.....
Run Code Online (Sandbox Code Playgroud)
如何添加以下案例?
我有同一段代码,我想从两个不同的地方执行。
代码 :
HttpClient client = new HttpClient();
client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", "...");
var post = client.PostAsync("https://mycompany.com/...", new StringContent(json, Encoding.UTF8, "application/json")).GetAwaiter().GetResult();
var str = post.Content.ReadAsStringAsync().GetAwaiter().GetResult();
Run Code Online (Sandbox Code Playgroud)
当将此代码放入控制台时,它工作正常并且我得到了预期的结果(Json in str)。
但是,当将其放入 Outlook VSTO 项目(在功能区部分)中时。它无法系统地告诉我连接已经关闭:
The underlying connection was closed: The connection was closed unexpectedly
在 VSTO 中,我尝试过:
client.CancelPendingRequests();没有成功另一方面,当请求另一个网站内容时,它可以工作(即:我将http://mycompany.com更改为https://google.com)。
任何想法?
我正在尝试运行 HttpClient 类的简单 Msdn 文章: 在 .Net Core 控制台应用程序中创建 Http 客户端。
不幸的是,它根本不起作用并退出应用程序。
输出中没有任何内容,只是在客户端调用中被吹走。
I am trying to get the response from a url, and when I use the await and async in my function, my Mutex throws an error.
Error output :
System.ApplicationException
Object synchronization method was called from an unsynchronized block of code.
at System.Threading.Mutex.ReleaseMutex()
Run Code Online (Sandbox Code Playgroud)
Code :
private async void getData ()
{
_mutex.WaitOne();
try
{
string url = "https://urllink.com";
HttpClient client = new HttpClient();
string response = await client.GetStringAsync(url);
}
catch (Exception e)
{
// TODO
throw e;
}
_mutex.ReleaseMutex();
}
Run Code Online (Sandbox Code Playgroud) 我正在尝试测试这种方法。
@Override
public JSON connectResource() throws IOException {
//get the location and credentials for the certificates
System.setProperty("javax.net.ssl.trustStore", "C:/Program Files/Java/jdk1.7.0_40/jre/lib/security/cacerts");
System.setProperty("javax.net.ssl.trustStorePassword", "changeit");
HttpRequest httpRequest = new HttpGet(url);
System.out.println("hello");
httpRequest.addHeader("Accept", "application/json");
HttpResponse response = httpClient.execute((HttpUriRequest) httpRequest);
System.out.println("hello1");
HttpEntity httpEntity = response.getEntity();
String data = this.getData(httpEntity);
return JSONSerializer.toJSON(data.toString());
}
Run Code Online (Sandbox Code Playgroud)
我的设置方法是:
@Before
public void setUp() throws Exception{
mockHttpClient = mock(DefaultHttpClient.class);
mockHttpRequest = mock(HttpUriRequest.class);
mockHttpResponse = mock(BasicHttpResponse.class);
mockHttpEntity = mock(HttpEntity.class);
mockInputStream = mock(InputStream.class);
mockInputStreamReader = mock(InputStreamReader.class);
mockBufferedReader = mock(BufferedReader.class);
mockHttpGet = mock(HttpGet.class);
mockHttpRequestBase = mock(HttpRequestBase.class); …Run Code Online (Sandbox Code Playgroud) 我想发送一个帖子请求,以便通过Google表单将数据发送到我的工作表
我使用Android Studio,在我的android项目中,当我尝试执行DefaultHttpClient对象时遇到错误:
---------崩溃开始10-26 16:19:08.749 21824-22874/mypackage E/AndroidRuntime:FATAL EXCEPTION:Thread-1553进程:mypackage,PID:21824 java.lang.NoSuchMethodError:No virtual方法执行(Lorg/apache/http/client/methods/HttpUriRequest; Lorg/apache/http/protocol/HttpContext;)Lorg/apache/http/client/methods/CloseableHttpResponse; 在类Lorg/apache/http/impl/client/DefaultHttpClient中; 或其超类('org.apache.http.impl.client.DefaultHttpClient'的声明出现在/system/framework/org.apache.http.legacy.boot.jar中)
错误报告给该行: response = httpClient.execute(httpPost,localContext);
我使用httpclient-4.5.1.jar库
minSDK:API 15:Android 4.0.3(IceCreamSandwich)
我不知道如何解决这个问题!请帮我!
我注意到,那HttpResponseMessage.Content是有ReadAsStringAsync()方法的。当此操作需要 cpu 并且任务反应会增加更多 CPU 工作时,使其异步有何意义?
我在Angular中进行http调用如下:
this.values = this.http.get("https://reqres.in/api/users/2").subscribe(data => console.log(data))
console.log(this.values)
Run Code Online (Sandbox Code Playgroud)
现在,第一行中的console.log打印数据变量,并打印正确的返回结果,即json格式数据。我想做的是将返回值保存在this.values变量中,但不起作用。this.value变量返回的对象中包含一些与我的返回值无关的奇怪信息。
下图显示了控制台日志:
this.values返回图片顶部的对象,而不是console.log(data)返回底部的json。
如何将http返回值保存到this.value变量中?
我正在尝试使用HttpClient.GetStreamAsync()方法下载文件。但是,这里有一个问题。GetStreamAsync()方法返回只读流,因此我无法使用Length属性声明IM试图读取字节的字节数组缓冲区。
using (var file = await _httpClient.GetStreamAsync(url).ConfigureAwait(false))
{
// it fails at file.Length below
byte[] blob = new byte[file.Length];
await file.ReadAsync(blob, 0, (int)file.Length).ConfigureAwait(false);
}
Run Code Online (Sandbox Code Playgroud) httpclient ×12
c# ×6
angular ×2
async-await ×2
http ×2
java ×2
.net ×1
.net-core ×1
android ×1
angular5 ×1
asynchronous ×1
google-forms ×1
http-request ×1
mockito ×1
models ×1
mutex ×1
post ×1
powermock ×1
rest ×1
rest-client ×1
rxjs ×1
stream ×1
task ×1
vsto ×1