2012-11-12 9 views
1

문제가 있습니다. 나는 게시물의 조치를하고 싶지 나는 이런 식으로 일을 해요 :POST 동작 - 매개 변수 없음

public partial class _Default : System.Web.UI.Page 
{ 
    protected void Page_Load(object sender, EventArgs e) 
    { 
     Response.Write(Caller.Process(Request.RawUrl, Request.QueryString, Request.UserHostAddress)); 
    } 
} 

그리고 내 가공 방법은 다음과 같이 : 이것은 내 default.aspx에 내 코드 behing이

string post_data = string.Format("taskId={0}&inputId={1}&value={2}", taskId, inputId, "101"); 

string uri = "http://localhost:60837/Default.aspx"; 
// Create a request using a URL that can receive a post. 
     WebRequest request = WebRequest.Create(uri); 
     // Set the Method property of the request to POST. 
     request.Method = "POST"; 
     // Create POST data and convert it to a byte array 
     var byteArray = Encoding.UTF8.GetBytes(post_data); 
     // Set the ContentType property of the WebRequest. 
     request.ContentType = "application/x-www-form-urlencoded"; 
     // Set the ContentLength property of the WebRequest. 
     request.ContentLength = byteArray.Length; 
     // Get the request stream. 
     Stream dataStream = request.GetRequestStream(); 
     // Write the data to the request stream. 
     dataStream.Write(byteArray, 0, byteArray.Length); 
     // Close the Stream object. 
     dataStream.Close(); 
     // Get the response. 
     WebResponse response = request.GetResponse(); 
     // Display the status. 
     Console.WriteLine(((HttpWebResponse)response).StatusDescription); 
     // Get the stream containing content returned by the server. 
     dataStream = response.GetResponseStream(); 
     // Open the stream using a StreamReader for easy access. 
     StreamReader reader = new StreamReader(dataStream); 
     // Read the content. 
     string responseFromServer = reader.ReadToEnd(); 
     // Display the content. 
     Console.WriteLine(responseFromServer); 
     // Clean up the streams. 
     reader.Close(); 
     dataStream.Close(); 
     response.Close(); 

입니다 : 정적 판독 전용 문자열 // paramDefinition = "정의" paramTaskId = "TASKID" paramInputId = "inputId"= "값" paramValue;

static public string Process(string query, NameValueCollection collection, string ip) 
    { 
     StringBuilder result = new StringBuilder(); 

     Func<string, bool> check = str => 
     { 
      if (!collection.AllKeys.Contains(str)) 
      { 
       result.AppendLine(string.Format("No {0} parameter. ", str)); 
       return false; 
      } 
      return true; 
     }; 
     if (check(paramTaskId) && check(paramInputId) && check(paramValue)) 
     { 
      result.Append("OK"); 
      Execute(query, collection, ip); 
     } 
     else 
     { 
      WriteLog(result.ToString(), query, ip); 
     } 

     return result.ToString(); 
    } 

문제는 내 Deafault.aspx에 매개 변수가 없습니다. 브라우저에서이 작업을 수행하면 모든 것이 정상입니다. 무엇이 문제가 될 수 있는지 알고 있습니까? 미리 감사드립니다.)

+0

브라우저에서 테스트 할 때'GET' 또는'POST'를 통해 전달합니까? 'Default.aspx'에있는 코드가'GET','POST' 또는 둘 다에서 변수를 찾습니까? – mellamokb

+0

브라우저에서 나는 http : // localhost : 60837/default.aspx? taskId = 3 & value = 10 & inputId = 44 –

+1

과 같은 텍스트를 입력합니다. 브라우저 메소드를 사용하여 변수를 'GET'으로 전달합니다. 코드를 사용하면 POST로 전달합니다. Request.QueryString [..] vs Request.Form [..]'을 찾으면 다르게 참조됩니다 (권장하지 않는 catch-all 메소드를 사용하지 않는 한). 'Default.aspx'에서 변수를 어떻게 검색하고 있습니까? 그것은 차이를 만들 수 있습니다. – mellamokb

답변

1

브라우저 메소드를 사용하면 변수를 GET (즉, URL의 일부로 전달 된 매개 변수 사용)으로 전달하게됩니다. 코드를 사용하면 POST을 전달합니다. 그것들은 두 가지 다른 것들이며, 서버 측에서 처리 할 때 다르게 처리됩니다.

Default.aspx의 코드에서 변수를 참조하면 GET 또는 URL에 전달 된 매개 변수를 구체적으로 찾는 Request.QueryString을 사용하고 있습니다. POST을 통해 전달 된 변수를 검색하려면 Request.Form["varName"]을 사용해야합니다.

+0

내 Process 메서드를 게시했습니다. 나는 그것이 올바르게하고 있다고 생각한다. –

+0

예,'Request.QueryString'을'Request.Form'으로 대체하십시오. – mellamokb

+0

대단히 고마워. :) 때때로 6 시간이 지나면 생각하기가 힘들어. :) –