我需要从一个Mvc4应用程序发送文件到另一个(另一个是Mvc4,WebApi应用程序).为了发送我使用HttpClient的PostAsync方法.以下是执行发送的代码:
public class HomeController : Controller
{
public async Task<ActionResult> Index()
{
var result =
await Upload("http://localhost/target/api/test/post", "Test", System.IO.File.Open(@"C:\SomeFile", FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite));
return View(result);
}
public async Task<String> Upload(String url, string filename, Stream stream)
{
using (var client = new HttpClient())
{
var formData = new MultipartFormDataContent();
var fileContent = new StreamContent(stream);
var header = new ContentDispositionHeaderValue("attachment") { FileName = filename };
fileContent.Headers.ContentDisposition = header;
formData.Add(fileContent);
var result = await client.PostAsync(url, formData); // Use your url here
return "123";
} …Run Code Online (Sandbox Code Playgroud) .net async-await asp.net-mvc-4 asp.net-web-api dotnet-httpclient
我是异步的新手,等待编程风格.如何解决以下问题:
我先调用下面的代码.这里的问题是第一行正在等待填充它的需要categoriesvm.Categorieslist,但它没有,但第二行被调用.(我认为这是await的默认行为)
如何确保仅categoriesvm.Categorieslist在第一行中填充第二行时才调用第二行?
protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
{
categoriesvm.GetCategories();
BookCategories.DataContext = from vm in categoriesvm.Categorieslist select vm;
}
Run Code Online (Sandbox Code Playgroud)
在上面的代码中,当我执行第一行时,它会在Categorieslist我上面访问的列表的下方.
public async void GetCategories()
{
Categorieslist = new ObservableCollection<Categories>(await PhoneClient.GetDefaultCategories());
}
Run Code Online (Sandbox Code Playgroud)
在phoneclient下面
public class PhoneClient
{
private static readonly HttpClient client;
public static Uri ServerBaseUri
{
get { return new Uri("http://169.254.80.80:30134/api/"); }
}
static PhoneClient()
{
client =new HttpClient();
client.MaxResponseContentBufferSize = 256000;
client.DefaultRequestHeaders.Add("user-agent", "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; WOW64; Trident/6.0)"); …Run Code Online (Sandbox Code Playgroud) 我有以下内容,我尝试从服务器下载字符串:
HttpClient client = new HttpClient();
var getResponsestring = await client.GetStringAsync("url");
Run Code Online (Sandbox Code Playgroud)
但是我如何去做,如果服务器没有返回我想要的字符串,但例如错误401?或者那个mather的任何网络错误
我正在寻找一个简单的示例,使用.net HttpClient POST参数并添加标题。这在RestSharp中非常容易,但是到目前为止,我还看不到如何使用HttpClient执行此操作的明确方法。
我使用httpclient来保存URI中的文件.pdf不会一致地保存pdf文件.我解决了这个问题,但我想知道是否有人能够解释为什么会发生这种情况.原始代码是:
using (var pdfStream = File.Create(savePdf))
result.Content.CopyToAsync(pdfStream);
Run Code Online (Sandbox Code Playgroud)
有效的代码是:
File.WriteAllText(savePdf, result.Content.ReadAsStringAsync().Result);
Run Code Online (Sandbox Code Playgroud) 我有一个PCl,我想在其中使用HttpClient进行异步调用.我编码像这样
public static async Task<string> GetRequest(string url)
{
var httpClient = new HttpClient() { MaxResponseContentBufferSize = int.MaxValue };
HttpResponseMessage response = await httpClient.GetAsync(url);
return response.Content.ReadAsStringAsync().Result;
}
Run Code Online (Sandbox Code Playgroud)
但等待显示错误"无法等待System.net.http.httpresponsemessage"之类的消息.
如果我使用这样的代码比一切顺利但不是异步方式
public static string GetRequest(string url)
{
var httpClient = new HttpClient() { MaxResponseContentBufferSize = int.MaxValue };
HttpResponseMessage response = httpClient.GetAsync(url).Result;
return response.Content.ReadAsStringAsync().Result;
}
Run Code Online (Sandbox Code Playgroud)
我只是希望这个方法以异步方式执行.
这是屏幕截图:

在我的应用程序中,我喜欢尝试并保持一致,并尽可能使用HttpClient.但是,有时我不需要HttpClient的异步属性,因此我只需在返回后立即获取任务的结果,如下面的代码所示.
public HttpResponseMessage httpPostWrapperMethod(string postBody, string url)
{
HttpContent content = new StringContent(postBody, Encoding.UTF8, "application/json");
HttpClient client = new HttpClient();
return client.PostAsync(url, content).Result;
}
Run Code Online (Sandbox Code Playgroud)
我的两个问题是:
这个代码在拨打电话时会导致新的线程在后台旋转吗?
和
如果我对这个其他服务的调用大约需要500毫秒,那么当服务处于大约100个请求/秒的生产负载时,这是否会导致我吃太多线程?
使用C#,我可以使用System.Net.Http库直接发布流,如下所示:
private async Task UploadFileAsync(Uri uri, string filename)
{
using (var stream = File.OpenRead(filename))
using (var client = new HttpClient())
{
await client.PostAsync(uri, new StreamContent(stream));
}
}
Run Code Online (Sandbox Code Playgroud)
有没有办法在功能上惯用的方式在F#中做到这一点?
FSharp.Data.Http.Request允许您发布二进制数据,但需要一个字节数组来执行此操作,这意味着将流读取到内存中.我希望避免这种情况,因为我发布了> 20Mb.
我在.NET Framework 4.7.1中有以下代码:
using (var handler = new HttpClientHandler())
{
handler.SslProtocols = SslProtocols.Tls12;
...
}
Run Code Online (Sandbox Code Playgroud)
此代码适用于我的开发计算机(Windows 10 Enterprise,Build 17101/rs4_release.180211-1040;版本1803; OS Build 17101.1000).
相同的代码(当发布到已安装.net framework 4.7.1的服务结构集群节点时)会导致以下异常:
System.PlatformNotSupportedException: Operation is not supported on this platform.
Run Code Online (Sandbox Code Playgroud)
群集节点正在运行Windows Server 2012 R2数据中心版本6.3(Build 9600)
使用Resharper反编译代码时,我的桌面显示以下信息:
// Type: System.Net.Http.HttpClientHandler
// Assembly: System.Net.Http, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
// MVID: 7D453494-012A-4FA4-9E04-1C64E3A0FB6F
// Assembly location: C:\Windows\Microsoft.NET\assembly\GAC_MSIL\System.Net.Http\\v4.0_4.0.0.0__b03f5f7f11d50a3a\\System.Net.Http.dll
Run Code Online (Sandbox Code Playgroud)
它显示在另一台本地服务器机器上:
// Type: System.Net.Http.HttpClientHandler
// Assembly: System.Net.Http, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
// MVID: 641F4770-704A-420A-A419-A4FAF4195ADD
// Assembly location: C:\Windows\Microsoft.NET\Framework\\v4.0.30319\\System.Net.Http.dll
Run Code Online (Sandbox Code Playgroud)
如您所见,装配位置不同,但我不知道这意味着什么.此外,我的本地计算机具有此注册表项值:
"Software\\Microsoft\\NET Framework Setup\\NDP\\v4\\Full\\Release": "461802"
Run Code Online (Sandbox Code Playgroud)
和群集节点有这个:
"Software\\Microsoft\\NET Framework …Run Code Online (Sandbox Code Playgroud) C#报表中的新手
可以说我有以下代码
HttpResponseMessage response = ...
Run Code Online (Sandbox Code Playgroud)
我如何检查状态代码response是否为403?
该属性StatusCode是一个对象-与整数相对,所以我不太确定该怎么做。