Click here to Skip to main content
11,431,844 members (62,798 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#4.0
I have an Classic ASP site, and I need to convert some POST method into asp.net. I am having some trouble understanding exactly what is happening here.

The Classic code is as follow:
<%Function PostHTTP(strURL, strBody, strErrTemplate)
    ON ERROR RESUME NEXT
    Dim objHTTP, strResult
    
    Set objHTTP = Server.CreateObject("Msxml2.ServerXMLHTTP.3.0")
    
    If Err.Number <> 0 Then
      strResult = Replace(strErrTemplate, "%1", Err.Number)
      strResult = Replace(strResult, "%2", Err.Description)
      strResult = Replace(strResult, "%3", "Init::" & Err.Source)
      Set objHTTP = Nothing
      PostHTTP = strstrResult
      Exit Function
    End If
        
    With objHTTP
      .Open "POST", strURL, False
      .setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
      .setTimeouts 30000, 30000, 60000, 240000
      .send strBody
      
      If Err.Number <> 0 Then
        strResult = Replace(strErrTemplate, "%1", Err.Number)
        strResult = Replace(strResult, "%2", Err.Description)
        strResult = Replace(strResult, "%3", "Post::" & Err.Source)
      Else
        strResult = .responseText
      End If
    End With
    
   ' Response.Write "strResult: " & strResult
  'Response.End
  
    
    If Err.Number > 0 Then
      strResult = Replace(strErrTemplate, "%1", Err.Number)
      strResult = Replace(strResult, "%2", Err.Description)
      strResult = Replace(strResult, "%3", Err.Source)
    ElseIf Len(strResult) = 0 Then
      strResult = Replace(strErrTemplate, "%1", 2000)
      strResult = Replace(strResult, "%2", "No response received from remote server.")
      strResult = Replace(strResult, "%3", "PostHTTP")
    End If
    
    PostHTTP = strResult
    Set objHTTP = Nothing
  End Function

Below are the two methods of Posting I have tried so far:
Method 1:
string URL = ConfigurationManager.AppSettings["strHE_URL"].ToString();
 
                byte[] data = System.Text.UTF8Encoding.UTF8.GetBytes(Xml);
 
                request = (HttpWebRequest)WebRequest.Create(URL);
                request.Method = "POST";
 
                request.ContentType = "application/x-www-form-urlencoded";
                request.ContentLength = data.Length;
                request.Timeout = 60000;
                request.ReadWriteTimeout = 60000;
 
                Stream stream = request.GetRequestStream();
 
                stream.Write(data, 0, data.Length);
                var sr = new StreamReader(request.GetResponse().GetResponseStream());
                string result = sr.ReadToEnd();
 
                sr.Close();
                stream.Close();

Method 2:

            using (WebClient wc = new WebClient())
            {
                wc.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded";
                string strResult = wc.UploadString(URL, Xml);
            }

Both of these methods fail when the response string should be returned. The operation times out after a minute.

What am I doing wrong here?

Any help would be appreciated.
Posted 3-Jan-13 22:34pm
Comments
DR Delphi at 6-Jan-13 12:18pm
   
are you converting this code to be used in VB.Net or C#.NET
Sheikh Muhammad Haris at 7-Jan-13 1:12am
   
Are you getting error

Stream stream = request.GetRequestStream();

OR

var sr = new StreamReader(request.GetResponse().GetResponseStream());

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



Advertise | Privacy | Mobile
Web03 | 2.8.150428.2 | Last Updated 4 Jan 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100