2009-09-08 56 views
1

我有一个恼人的ASP.NET问题:http POST与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"; 
} 

我的假设是,像这样的代码在小程序中使用:

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

不,的Request.Form不起作用。首先,我没有名字,并且索引1和索引2存在,但没有进一步...

但是我刚刚发现了我在整个周末都没有发现的东西: 它通过阅读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