在 C# 中向 POST 添加一个循环


如何

修改以下代码以循环访问文本文件而不是静态的"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:TestDirTest.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循环中移动这两行来完成的

相关内容

最新更新