使用asp.net进行http POST

时间:2009-09-08 12:52:46

标签: asp.net vb.net http cgi

我有一个恼人的ASP.NET问题:

我有一个Perl脚本(见下文),它获取了form_info变量。 现在不幸的是,它是http POST,而不是http GET,所以Request.Querystring不起作用......

现在我必须用asp.net页面/ app替换Perl脚本,但我的问题是当我没有字符串时我无法处理字符串form_info ... 我无法将http POST更改为HTTP get,因为它是由第三方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";
}

我的假设是,这样的代码在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) {}
    }

  }
}

2 个答案:

答案 0 :(得分:1)

你不能只使用Request.Form代替Request.QueryString吗?

例如:

string p = Request.Form["p"];    // should contain "java2s" in your example

答案 1 :(得分:1)

不,request.form不起作用。首先,我没有名字,索引一和二存在,但没有进一步......

但我刚刚发现了整个周末没有发现的东西: 它的工作原理是读取stdin,这相当于在asp.net中读取http头文件

我已经看到了一个asp.net http post请求的示例,并通过使用请求者对象的GetRequest方法读取流来找到答案。我没有任何请求者对象,但我意识到它可能在页面对象中的某个位置。由于搜索到的对象是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