2012-01-19 163 views
0

我目前正在使用asp.net中的通用處理程序。我相信它目前處於等待POST的「聆聽」狀態。我期待一個帶有文件名Header的POST,一旦收到處理程序將處理下載文件。我的處理程序代碼如下:HTTP POST要自測試處理程序

Sub ProcessRequest(Byval context as HttpContext) Implements IHttpHanlder.ProcessRequest 

If context.Request.HttpMethod() = "POST" Then 
Dim Reader as New StreamReader(context.Request.InputStream) 
Dim contents as String = reader.ReadtoEnd() 

Dim filename as String = context.Request.Headers(("filename")) 
System.IO.File.Writealltext(ConfigurationManager.AppSettings("outputdirectory"), contents) 
Else 
context.Response.ContentType = "text/plain" 
context.Response.ContentType("Handler is alive") 
End If 

End Sub 

我想複製一篇文章,看看它是否需要它成功。是否有可能從我的機器上的其他程序生成併發送此文章。我已經嘗試了一些教程包括本

SO - POST Tutorial

我覺得我得到的最接近使用該代碼(從以前的鏈接)

Using wc as New System.Net.WebClient 
wc.UploadFile("http://localhost:Port/local/of/handler", "C:\local\of\file.txt") 
End Using 

我從遠程接收500錯誤服務器。這是處理程序代碼的問題嗎?或者我只是沒有做出正確的POST類型?

當wc.Headers()/ Darin的建議搞亂時,我仍然得到500錯誤。以下例外。

System.Net.WebException: The remote server returned an error: (500) Internal Server Error 
at System.Net.HttpWebRequest.GetResponse() 
at System.Net.Webclient.GetWebResponse(WebRequest request) 
at System.Net.WebClient.WebClientWriteStream.Dispose(Boolean disposing) 
at System.IO.Stream.Close() 
at System.IO.Stream.Dispose() 
at TestPOSTGETEXE.Form1.Button4_Click(Object sender, EventArgs e) in C:\blah\blah\..\..\..\ 
+0

你的客戶端代碼甚至沒有嘗試設置你的處理程序代碼預計頭。 –

+0

我將編輯我已經搞砸了諸如wc.Headers.Add之類的東西,但似乎並沒有得到它直 – sealz

+0

「搞亂」不如實際的代碼有幫助。 –

回答

1

嘗試這樣的:

Using wc As New System.Net.WebClient 
    wc.Headers("filename") = "file.txt" 
    Using writer = wc.OpenWrite("http://localhost:Port/local/of/handler") 
     Dim file = System.IO.File.ReadAllBytes("C:\local\of\file.txt") 
     writer.Write(file, 0, file.Length) 
    End Using 
End Using 
+0

我仍然收到500錯誤我將編輯完全例外 – sealz

+0

@ harper89,當你調試它時你進入處理程序有什麼異常?當您嘗試寫入文件內容時,似乎存在一些問題WriteAllText - 您確定路徑是否正確,並且該帳戶有足夠的權限寫入該文件夾?同樣在你的處理程序中,你似乎沒有使用文件名頭,你使用了一些'ConfigurationManager.AppSettings(「outputdirectory」)',它看起來更像是一個目錄名,而不是文件名,所以這可能是你的通用處理程序崩潰。你應該''Path.Combine'這個值與'filename'變量。 –

+0

回來做了一個更新,但看起來像你先弄清楚了。調試處理程序時,看起來我無法進入任何目錄。每個位置都會拋出「UnauthorizedAccessException」訪問路徑「C:\ dir \ to \ put \ stuff」被拒絕 – sealz