2014-12-19 90 views
1

我试图使用我送使用Python中Requests C#读取文件test.txt阅读文件,要求Python的发送

的test.txt

你好

Client.py

import requests 

if __name__ == '__main__': 
    url = 'http://localhost:1234/' 
    headers = {'auth':'myAuth'} 
    r = requests.post(url, headers=headers,files={'test.txt':open('./test.txt','rb')}) 
    print r.text 

然后我试图找回在C#服务器端的数据:

Server.cs

[...] 
public void ProcessRequest(HttpListenerContext context) 
{ 
    Stream mStream = myHttpRequest.InputStream; 
    int content_size = int.Parse(myHttpRequest.Headers["content-length"]); 
    byte[] data = new BinaryReader(mStream).ReadBytes(content_size); 
    string str = System.Text.Encoding.Default.GetString(data); 
    Console.WriteLine(str); 
} 

我从Console.WriteLine(str)得到的是: enter image description here

但我不设法得到只有Hello部分。我怎样才能做到这一点 ?

回答

0

我设法从here通过修改下列行工作得到代码:

// Skip four lines (Boundary, Content-Disposition, Content-Type, and a blank) 
for (Int32 i = 0; i < 4; i++) 

// Skip 3 lines (Boundary, Content-Disposition and a blank) 
for (Int32 i = 0; i < 3; i++)