views:

263

answers:

2

I've an annoying ASP.NET problem:

I have a Perl script (see below), which gets the form_info variable. Now unfortunately, it's http POST, and not http GET, so Request.Querystring doesn't work...

Now I have to replace the Perl Script with an asp.net page/app, but my problem is that I cannot process the string form_info when I don't have the string... and I cannot change the http POST to a HTTP get, since it's generated by a 3rd party java applet.

# Print out a content-type for HTTP/1.0 compatibility
print "Content-type: text/html\n\n";
#
#test whether it's via a firewall (i.e. GET multiple times)
# or direct, i.e. POST
$method = $ENV{'REQUEST_METHOD'};
if ($method eq "GET") {    
    $form_info = $ENV{'QUERY_STRING'};
 print LOGFILE "Method found was: REQUEST_METHOD\n";
}
elsif ($method eq "POST"){
    # Get the input
    $data_size = $ENV{'CONTENT_LENGTH'};
    read(STDIN,$form_info,$data_size);
 print LOGFILE "\nMethod found was: POST\n";
}
else {
    print "Client used unsupported method";
 print LOGFILE "\nMethod found was: Client used unsupported method\n";
}

my assumption is, some code like this is used in the applet:

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.PostMethod;

import java.io.BufferedReader;
import java.io.InputStreamReader;

public class PostMethodExample {

  public static void main(String args[]) {

    HttpClient client = new HttpClient();
    client.getParams().setParameter("http.useragent", "Test Client");

    BufferedReader br = null;

    PostMethod method = new PostMethod("http://search.yahoo.com/search");
    method.addParameter("p", "\"java2s\"");

    try{
      int returnCode = client.executeMethod(method);

      if(returnCode == HttpStatus.SC_NOT_IMPLEMENTED) {
        System.err.println("The Post method is not implemented by this URI");
        // still consume the response body
        method.getResponseBodyAsString();
      } else {
        br = new BufferedReader(new InputStreamReader(method.getResponseBodyAsStream()));
        String readLine;
        while(((readLine = br.readLine()) != null)) {
          System.err.println(readLine);
      }
      }
    } catch (Exception e) {
      System.err.println(e);
    } finally {
      method.releaseConnection();
      if(br != null) try { br.close(); } catch (Exception fe) {}
    }

  }
}
+1  A: 

Can't you just use Request.Form instead of Request.QueryString?

For example:

string p = Request.Form["p"];    // should contain "java2s" in your example
LukeH
+1  A: 

No, request.form doesn't work. First, I don't have a name, and index one and two exist, but no further...

But I just found out what I didn't found out in an entire weekend: It works by reading stdin, which is equivalent to reading the http headers in asp.net

I've seen an example for an asp.net http post request, and found the answer read by reading a stream by using the GetRequest method of the requester object. I don't have any requestor object, but I realized it might be somewhere in the page object. the logical place seamed to be request, since the object searched is GetRequest.

    Function readme() As String
    Dim sr As System.IO.StreamReader = New System.IO.StreamReader(Page.Request.InputStream())
    Return sr.ReadToEnd().Trim()
End Function


Sub WriteToFile(Optional ByRef strStringToWrite As String = "Hello World")
    Dim fp As System.IO.StreamWriter

    Try
        fp = System.IO.File.CreateText(Server.MapPath("./") & "test.txt")
        fp.WriteLine(strStringToWrite)
        Response.Write("File Succesfully created!")
        fp.Close()
    Catch err As Exception
        Response.Write("File Creation failed. Reason is as follows " + err.ToString())
    Finally

    End Try
End Sub

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    'Response.Write(readme)


    WriteToFile(readme())

    'Dim p As String = Request.Form(0)
    'WriteToFile(p)

    'p = Request.Form(2)
    'WriteToFile(p)

End Sub

Quandary