[英]Add a loop to a POST in C#
如何修改以下代碼以遍歷文本文件而不是靜態“ testData”字符串? 第一個代碼塊POST可以工作,我嘗試使用循環是下面的第二個代碼塊,但是它不起作用。
<%@ Page Language="C#" Debug="true" %>
<%@ Import Namespace="System.Net"%>
<%@ Import Namespace="System.IO"%>
<%
string url = "http://testurl.com/test";
HttpWebRequest request = HttpWebRequest.Create(url) as HttpWebRequest;
HttpWebResponse response = null;
string user = "testuser";
string password = "testpassword";
string testData ="id=003&email=test@hotmail.com&firstname=Test&lastname=User";
byte[] byteData = Encoding.UTF8.GetBytes(mydata);
UTF8Encoding enc = new UTF8Encoding();
request.Method = "POST";
string auth = "Basic " + Convert.ToBase64String(System.Text.Encoding.Default.GetBytes(user + ":" + password));
request.PreAuthenticate = true;
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = byteData.Length;
request.Accept = "application/json";
request.AuthenticationLevel = System.Net.Security.AuthenticationLevel.MutualAuthRequested;
request.Headers.Add("Authorization", auth);
using (Stream ds = request.GetRequestStream())
{
ds.Write(byteData, 0, byteData.Length);
ds.Close();
}
try
{
response = (HttpWebResponse)request.GetResponse();
}
catch (WebException ex)
{
response = (HttpWebResponse)ex.Response;
}
Stream receiveStream = response.GetResponseStream();
StreamReader reader = new StreamReader(receiveStream, Encoding.UTF8);
string content = reader.ReadToEnd();
Response.Write(content);
%>
以下內容不起作用,在ContentLength行上出現錯誤“寫入開始后無法設置此屬性”
<%@ Page Language="C#" Debug="true" %>
<%@ Import Namespace="System.Net"%>
<%@ Import Namespace="System.IO"%>
<%
string url = "http://testurl.com/test";
HttpWebRequest req = HttpWebRequest.Create(url) as HttpWebRequest;
HttpWebResponse wr = null;
string user = "testuser";
string pwd = "testpassword";
string fileName = @"C:\TestDir\Test.txt";
string[] lines = System.IO.File.ReadAllLines(fileName);
foreach (string line in lines)
{
string mydata = line;
byte[] byteData = Encoding.UTF8.GetBytes(mydata);
UTF8Encoding enc = new UTF8Encoding();
req.Method = "POST";
string auth = "Basic " + Convert.ToBase64String(System.Text.Encoding.Default.GetBytes(user + ":" + pwd));
req.PreAuthenticate = true;
req.ContentType = "application/x-www-form-urlencoded";
req.ContentLength = byteData.Length;
req.Accept = "application/json";
req.AuthenticationLevel = System.Net.Security.AuthenticationLevel.MutualAuthRequested;
req.Headers.Add("Authorization", auth);
using (Stream ds = req.GetRequestStream())
{
ds.Write(byteData, 0, byteData.Length);
ds.Close();
}
try
{
wr = (HttpWebResponse)req.GetResponse();
}
catch (WebException ex)
{
wr = (HttpWebResponse)ex.Response;
}
Stream receiveStream = wr.GetResponseStream();
StreamReader reader = new StreamReader(receiveStream, Encoding.UTF8);
string content = reader.ReadToEnd();
Response.Write(content);
}
%>
將以下兩行移動到您的foreach
循環中
HttpWebRequest req = HttpWebRequest.Create(url) as HttpWebRequest;
HttpWebResponse wr = null;
發生的情況是您執行一個請求,然后循環再次執行,並且該請求已被使用,因此當您觸摸敏感的ContenLength
屬性時,請求不會處於可以修改的狀態。 您需要創建一個全新的請求,這將通過在foreach
循環內移動這兩行來完成
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.