我真的找不到一个可行的例子,也许是因为它根本不可能?
我想要一个 C# 匿名类型对象,如下所示:
var postBody = new
{
friend = new
{
name = "dave",
last ="franz"
},
id = "12345",
login = "mylogin"
};
Run Code Online (Sandbox Code Playgroud)
并通过简单的 http POST 将其发布到我的 Web 服务,帖子正文如下:
{
"friend" :
{
"name" : "dave",
"last" : "franz"
},
"id" : "12345",
"login" : "mylogin"
};
Run Code Online (Sandbox Code Playgroud) 大家好,我在 WebClient 中遇到问题问题是我无法使用 WebClient.downloadFile 或 WebClient.DownloadFileAsync 下载特定文件,错误是 [请求已中止:无法创建 SSL/TLS 安全通道] 和下载的文件是 0bytes 请帮助我下载文件或为我的问题提供解决方案的任何代码我的代码是:
WebClient Client = new WebClient();
Client.DownloadFile("https://github.com/MohammedZr/roepo/blob/master/x82bit/32bit.exe", @"C:\Users\MohamedAlzurghni\Desktop\Project\bolo\test\32bit.exe");
Run Code Online (Sandbox Code Playgroud) 考虑以下代码:
public Mono<Void> doStuff() {
return this.requestStuff()
.onStatus(HttpStatus::is5xxServerError,
clientResponse -> {
aMethodIWouldLikeToTest(clientResponse);
return Mono.error(new MyCustomException("First error I would like to test"));
})
.onStatus(HttpStatus::is4xxClientError,
clientResponse -> {
aMethodIWouldLikeToTest(clientResponse);
return Mono.error(new MyCustomException("Second error I would like to test"));
})
.bodyToMono(String.class)
.flatMap(x -> anotherMethodIManagedToTest(x)))
}
Run Code Online (Sandbox Code Playgroud)
我的第一个目标是测试anotherMethodIManagedToTest(x),这是使用以下方法实现的:
import org.springframework.web.reactive.function.client.WebClient;
...
@Mock
private WebClient.ResponseSpec responseSpec;
private String desiredInputParam = "There is another black spot on the sun today!";
...
@Test
public void allGood_anotherMethodIManagedToTest_success {
...
ClassUnderTest classUnderTest = new classUnderTest()
ClassUnderTest classUnderTestSpy = …Run Code Online (Sandbox Code Playgroud) 我正在尝试在 Spring Boot 中使用WebClient发出API POST 请求。但我无法发出请求并接收JSONObject形式的响应。用RestTemplate我就做到了,最近开始学习WebClient。所以我被困住了。
错误 Spring 给出: Error:(48, 28) java: 不兼容的类型:不存在类型变量 T 的实例,因此reactor.core.publisher.Mono 符合 org.json.simple.JSONObject
这是我的源代码:
控制器.java
JSONObject jsonObject = new JSONObject();
Turnover turnover = new Turnover();
JSONObject resp = webClientBuilder.build()
.post()
.uri("http://180.12.10.10:8080/turnover/")
.contentType(MediaType.APPLICATION_FORM_URLENCODED)
.accept(MediaType.APPLICATION_JSON )
.body(Mono.just(turnover),Turnover.class)
.retrieve()
.bodyToMono(JSONObject.class);
Run Code Online (Sandbox Code Playgroud)
营业额.java
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
public class Turnover {
private String start_date;
private String end_date;
private String account;
public Turnover(){
setStart_date("01.01.2020");
setEnd_date("01.06.2020");
setAccount("20296");
}
}
Run Code Online (Sandbox Code Playgroud)
我要发送的Json
{
"start_date":"01.01.2020",
"end_date":"01.06.2020",
"account":"20296"
}
Run Code Online (Sandbox Code Playgroud)
响应API返回:
{
"status": …Run Code Online (Sandbox Code Playgroud) 我正在尝试将多个标头放入 defaultHeaders(),但我不知道如何从 createHeaders() 方法的返回值中创建 Consumer 对象
this.someWebClient = WebClient.builder()
.baseUrl(someConfiguration.getApiUrl())
.clientConnector(buildTimeoutConnector())
.defaultHeaders(????) // Consumer<HttpHeaders>
.build();
Run Code Online (Sandbox Code Playgroud)
我可以用这种方式构建我的标题:
private HttpHeaders createHeaders(String token) {
HttpHeaders headers = new HttpHeaders();
headers.add(HttpHeaders.ACCEPT, V1_PUBLIC);
headers.add(HttpHeaders.HOST, "abc");
headers.add(HttpHeaders.AUTHORIZATION, "Bearer " + token);
return headers;
}
Run Code Online (Sandbox Code Playgroud)
但是如何将其包装到消费者中?
我有一个WPF应用程序,我想下载一个文件.
我正在使用System.Net; 我有以下代码:
WebClient ww = new WebClient();
ww.DownloadFileAsync(
new Uri("http://www.sinvise.net/tester/1.jpg"),
AppDomain.CurrentDomain.BaseDirectory + "\\1.jpg");
Run Code Online (Sandbox Code Playgroud)
问题是,它是不下载文件,它只是显示为0kb文件而不是下载,我不知道是什么问题,任何人都可以帮忙吗?
这个方法我用于并发下载.
public void DownloadConcurrent(Action Method)
{
Action[] methodList = new Action[Concurent_Downloads];
for (int i = 0; i < Concurent_Downloads; i++)
{
methodList[i] = Method;
}
Parallel.Invoke(methodList);
}
Run Code Online (Sandbox Code Playgroud)
我正在尝试同时下载网址,但有效下载的数量始终为1.
就像所有下载都会调用一样,但只有一个url会开始下载数据,而不是所有下载都会开始下载.
我希望所有下载同时并行工作,无法实现.
更新:方法正在使用队列,它正在下载不同的URL,形成队列.
我想在Windows Phone应用程序中非异步下载数据.我会创建一个下载程序类,并在其中有一个简单的方法从URL下载字符串.在其他平台上,我会使用:
public class TextDownloader
{
public string GetString(string url)
{
WebClient web = new WebClient();
string s = web.DownloadString("http://www.google.com");
return s;
}
}
Run Code Online (Sandbox Code Playgroud)
它会很好用:简单,最小的代码.但是,该WebClient.DownloadString方法在Windows Phone 7上不可用,也没有很多WebRequest选项.有没有其他方法可以在Windows Phone中异步下载数据?我宁愿不必为下载和错误创建多个事件,只需要一个简单的方法返回一个值或抛出一个异常.
所以我昨晚刚开始学习C#.我开始的第一个项目是一个简单的Image-Downloader,它使用HtmlElementCollection下载网站的所有图像.
这是我到目前为止所得到的:
private void dl_Click(object sender, EventArgs e)
{
System.Net.WebClient wClient = new System.Net.WebClient();
HtmlElementCollection hecImages = Browser.Document.GetElementsByTagName("img");
for (int i = 0; i < hecImages.Count - 1; i++)
{
char[] ftype = new char[4];
string gtype;
try
{
//filetype
hecImages[i].GetAttribute("src").CopyTo(hecImages[i].GetAttribute("src").Length -4,ftype,0,4) ;
gtype = new string(ftype);
//copy image to local path
wClient.DownloadFile(hecImages[i].GetAttribute("src"), absPath + i.ToString() + gtype);
}
catch (System.Net.WebException)
{
expand_Exception_Log();
System.Threading.Thread.Sleep(50);
}
Run Code Online (Sandbox Code Playgroud)
基本上它是提前渲染页面并寻找图像.这很好用,但由于某种原因它只下载缩略图,而不是完整(高分辨率)图像.
其他来源:
WebClient.DownloadFile上的文档:http://msdn.microsoft.com/en-us/library/ez801hhe( v = vs.110).aspx
DownloadFile方法从address参数中指定的URI下载到本地文件数据.