我需要使用WebClient的"HTTP Post"将一些数据发布到我拥有的特定URL.
现在,我知道这可以通过WebRequest完成,但出于某些原因我想使用WebClient.那可能吗?如果是这样,有人能告诉我一些例子还是指向正确的方向?
HttpValueCollection和之间有什么区别NameValueCollection?
如果可能,请举例说明.
谢谢
我有一个Web Api控制器.它表现得很奇怪.当我使用PostMan时,我可以在Web Api上访问POST方法,但是当我从.net使用HttpWebRequest时,它返回(405)Method Not Allowed.我把Web Api代码放在这里:
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web;
using System.Web.Http;
namespace MyProject.Controllers
{
public class TestController : ApiController
{
[HttpPost]
public int buy(OrderResult value)
{
try
{
if (value.InvoiceDate != null && value.Result == 1)
{
return 0;
}
}
catch{}
return -1;
}
}
public class OrderResult
{
public int Result { get; set; }
public long InvoiceNumber { get; set; }
public string InvoiceDate { get; …Run Code Online (Sandbox Code Playgroud) 简单的问题:
现在我在我看来有这种形式:
<form action="https://www.xyz.com/xyz/ISO/NewPayment"
method="post" name="payform">
<input type="hidden" name="session_id" value="@Model.OrderId">
<input type="submit" value="Pay">
</form>
Run Code Online (Sandbox Code Playgroud)
它发送POST数据并将用户重定向到https://www.xyz.com/xyz/ISO/NewPayment.
这么简单,但我想在用户被重定向到外部URL之前检查服务器端数据的正确性.
我怎么能在asp.net MVC中这样做?如何从服务器端发送POST数据,然后重定向用户?
问候
我正在尝试制作一个程序,通过 POST 中的用户名、密码、硬件 ID 和密钥来请求我的网站。
我这里有这段代码,应该使用该表单数据向我的网站发送 POST 请求,但是当它发送时,我的网络服务器报告说它没有收到 POST 数据
try
{
string poststring = String.Format("username={0}&password={1}&key={2}&hwid={3}", Username, Password, "272453745345934756392485764589", GetHardwareID());
HttpWebRequest httpRequest =
(HttpWebRequest)WebRequest.Create("mywebsite");
httpRequest.Method = "POST";
httpRequest.ContentType = "application/x-www-form-urlencoded";
byte[] bytedata = Encoding.UTF8.GetBytes(poststring);
httpRequest.ContentLength = bytedata.Length;
Stream requestStream = httpRequest.GetRequestStream();
requestStream.Write(bytedata, 0, bytedata.Length);
requestStream.Close();
HttpWebResponse httpWebResponse =
(HttpWebResponse)httpRequest.GetResponse();
Stream responseStream = httpWebResponse.GetResponseStream();
StringBuilder sb = new StringBuilder();
using (StreamReader reader =
new StreamReader(responseStream, System.Text.Encoding.UTF8))
{
string line;
while ((line = reader.ReadLine()) != null)
{
sb.Append(line);
}
}
return sb.ToString(); …Run Code Online (Sandbox Code Playgroud) .NET框架中是否有一个类,如果给定一个序列/可枚举的键值对(或者其他任何我对此格式不严格)可以创建如下的查询字符串:
?foo=bar&gar=har&print=1
Run Code Online (Sandbox Code Playgroud)
我自己可以完成这个微不足道的任务,但我想我会要求自己避免重新发明轮子.当单行代码可以做到的时候为什么所有那些弦乐体操呢?