读取使用Requests Python发送的C#中的文件

时间:2014-12-19 00:55:04

标签: c# python-requests httplistenerrequest

我正在尝试使用在Python中使用Requests发送的C#来读取文件test.txt

的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部分。我怎么能这样做?

1 个答案:

答案 0 :(得分: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++)