C#需要一些关于HttpWebRequest发布数据的帮助
我基本上知道如何使用HttpWebRequests,但我是个新手。所以我想用Post方法提交以下令牌C#需要一些关于HttpWebRequest发布数据的帮助,c#,post,httpwebrequest,C#,Post,Httpwebrequest,我基本上知道如何使用HttpWebRequests,但我是个新手。所以我想用Post方法提交以下令牌 authenticity_token=pkhn7pwt3QATOpOAfBERZ%2BRIJ7oBEqGFpnF0Ir4RtJg%3D&question%5Bquestion_text%5D=TEST+TEST+TEST&authenticity_token=pkhn7pwt3QATOpOAfBERZ%2BRIJ7oBEqGFpnF0Ir4RtJg%3D 它所做的是单击一个按钮
authenticity_token=pkhn7pwt3QATOpOAfBERZ%2BRIJ7oBEqGFpnF0Ir4RtJg%3D&question%5Bquestion_text%5D=TEST+TEST+TEST&authenticity_token=pkhn7pwt3QATOpOAfBERZ%2BRIJ7oBEqGFpnF0Ir4RtJg%3D
它所做的是单击一个按钮并发送文本“TEST”,这是当我单击想要发送的按钮时从firebug获得的令牌。要通过Http Post请求发送一些数据,您可以尝试使用以下代码: 检查服务器响应的“var serverResponse”
string targetUrl = "http://www.url.url";
var postBytes = Encoding.Default.GetBytes(@"authenticity_token=pkhn7pwt3QATOpOAfBERZ%2BRIJ7oBEqGFpnF0Ir4RtJg%3D&question%5Bquestion_text%5D=TEST+TEST+TEST&authenticity_token=pkhn7pwt3QATOpOAfBERZ%2BRIJ7oBEqGFpnF0Ir4RtJg%3D");
var httpRequest = (HttpWebRequest)WebRequest.Create(targetUrl);
httpRequest.ContentLength = postBytes.Length;
httpRequest.Method = "POST";
using (var requestStream = httpRequest.GetRequestStream())
requestStream.Write(postBytes, 0, postBytes.Length);
var httpResponse = httpRequest.GetResponse();
using (var responseStream = httpResponse.GetResponseStream())
if (responseStream != null)
using (var responseStreamReader = new StreamReader(responseStream))
{
var serverResponse = responseStreamReader.ReadToEnd();
}
另一个解决方案:
// you can get the correct encoding from your site's response headers
Encoding encoding = Encoding.UTF8;
string targetUrl = "http://example.com";
var request = (HttpWebRequest)WebRequest.Create(targetUrl);
var formData = new Dictionary<string, object>();
formData["authenticity_token"] = "pkhn7pwt3QATOpOAfBERZ+RIJ7oBEqGFpnF0Ir4RtJg=";
formData["question[question_text]"] = "TEST TEST TEST";
bool isFirstField = true;
StringBuilder query = new StringBuilder();
foreach (KeyValuePair<string, object> field in formData)
{
if (!isFirstField)
query.Append("&");
else
isFirstField= false;
query.AppendFormat("{0}={1}", field.Key, field.Value);
}
string urlEncodedQuery = Uri.EscapeDataString(query.ToString());
byte[] postData = encoding.GetBytes(urlEncodedQuery);
request.ContentLength = postData.Length;
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
using (BinaryWriter bw = new BinaryWriter(request.GetRequestStream()))
bw.Write(postData);
var response = request.GetResponse() as HttpWebResponse;
// TODO: process response
//您可以从站点的响应标题中获得正确的编码
编码=Encoding.UTF8;
字符串targetUrl=”http://example.com";
var request=(HttpWebRequest)WebRequest.Create(targetUrl);
var formData=新字典();
formData[“真实性令牌”]=“pkhn7pwt3qatopoafferz+RIJ7oBEqGFpnF0Ir4RtJg=”;
formData[“问题[问题文本]”]=“测试”;
bool isFirstField=true;
StringBuilder查询=新建StringBuilder();
foreach(formData中的KeyValuePair字段)
{
如果(!isFirstField)
查询。追加(“&”);
其他的
isFirstField=false;
AppendFormat(“{0}={1}”,field.Key,field.Value);
}
字符串urlEncodedQuery=Uri.EscapeDataString(query.ToString());
byte[]postData=encoding.GetBytes(urlEncodedQuery);
request.ContentLength=postData.Length;
request.Method=“POST”;
request.ContentType=“application/x-www-form-urlencoded”;
使用(BinaryWriter bw=new BinaryWriter(request.GetRequestStream()))
写入(postData);
var response=request.GetResponse()作为HttpWebResponse;
//TODO:进程响应
联机使用(var requestStream=httpRequest.GetRequestStream())我收到以下错误:无法发送具有此动词类型的内容正文。我的问题是,我忘记将httpRequest.Method设置为POST,请参阅更新的代码。