2014-02-19 3 views
1

정적 'testData'문자열 대신 텍스트 파일을 반복하여 다음 코드를 수정할 수 있습니까? POST 코드의 첫 번째 블록은 작동하지만 루프를 사용하려는 시도는 두 번째 코드 블록이지만 작동하지 않습니다.C에서 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 protected]&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); 
%> 

하면, 내가

<%@ 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); 
    } 
%> 
+2

foreach 내부에서 새로운 요청을해야한다고 생각합니다. – Gus

+0

감사합니다. –

답변

2
foreach 루프 내부에 다음 두 줄을 이동

ContentLength 라인에 '쓰기가 시작된 후이 속성을 설정할 수 없습니다'오류를 얻을 작동하지 않습니다 다음

HttpWebRequest req = HttpWebRequest.Create(url) as HttpWebRequest; 
HttpWebResponse wr = null; 

무슨 일이 일어나고하면 하나 개의 요청을 할 것입니다, 다음 루프는 다시 가고 요청이 이미 사용하고있다, 그래서 당신은 민감한 터치하면 ContenLength 속성입니다. 요청을 수정할 수있는 상태가 아닙니다. 새로운 요청을 만들려면 foreach 루프 내에서 두 줄을 이동하여 수행해야 할 작업이 무엇입니까

+0

감사합니다. 설명해 주셔서 감사합니다. –

+0

@ kyle_13, 오신 것을 환영합니다! – LB2