Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/307.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# C代码的Curl查询_C#_Curl - Fatal编程技术网

C# C代码的Curl查询

C# C代码的Curl查询,c#,curl,C#,Curl,我有一个很简单的问题 我想将curl查询“转换”为httpc客户端请求 我的查询是: curl -k -X POST -H "Content-Type: multipart/form-data" -H "X-Cookie: mycookie" -F "Filename=myfilename" -F "Filedata=@filepath" http://website... 你能帮我个忙吗 我试图编码: using (var client = new HttpClien

我有一个很简单的问题

我想将curl查询“转换”为httpc客户端请求

我的查询是:

curl -k -X POST -H "Content-Type: multipart/form-data" 
     -H "X-Cookie: mycookie" 
     -F "Filename=myfilename" -F "Filedata=@filepath" http://website...
你能帮我个忙吗

我试图编码:

using (var client = new HttpClient())
{
    using (var multipartFormDataContent = new MultipartFormDataContent())
    {
        client.DefaultRequestHeaders.Add("X-Cookie", string.Format("token={0}", token));

        multipartFormDataContent.Add(new StringContent("Compliance"), "Filename");
        multipartFormDataContent.Add(new ByteArrayContent(File.ReadAllBytes(filePath)), "Filedata", "Compliance");

        // Add a new Request Message
        HttpRequestMessage requestMessage = new HttpRequestMessage(HttpMethod.Post, yourUrl);

        // Add our custom headers
        requestMessage.Headers.Add("X-Cookie", string.Format("token={0}", token));
        requestMessage.Content = multipartFormDataContent;
        requestMessage.Content.Headers.ContentType = new MediaTypeHeaderValue("multipart/form-data");

        // Send the request to the server
        var response = client.SendAsync(requestMessage).Result; 
    }
}
我还尝试了以下代码:

var postData = new List<KeyValuePair<string, string>>();
                  postData.Add(new KeyValuePair<string, string>("Filename", "myfilename"));
                  postData.Add(new KeyValuePair<string, string>("Filedata", filepath));
                  HttpContent content = new FormUrlEncodedContent(postData);

                  HttpClient httpClient = new HttpClient();
                  httpClient.BaseAddress = new Uri("https://mywebsite");
                  httpClient.DefaultRequestHeaders.Add("X-Cookie", string.Format("token={0}", token));
                  var a = httpClient.PostAsync("/file/upload", content).Result;
我收到了错误信息:

远程服务器返回错误:500内部服务器错误

====


您能告诉我应该在代码中更正什么吗?或者您可以告诉我应该如何更正吗?

好的,那么您已经对代码进行了编码,并且?你有错误吗?只是没有回应?您面临的问题是什么?我收到此错误:response2={StatusCode:500,ReasonPhrase:'Unknown error',Version:1.1,Content:System.Net.Http.StreamContent,Headers:{Connection:close X-Frame-Options:DENY Date:Sat,2015年1月24日21:44:37 GMT内容长度:37内容类型。。。
 string input = "Filename=myfilename&Filedata=@" + filepath;
        var request = (HttpWebRequest)WebRequest.Create(yourUrl);
        request.Headers.Add("X-Cookie", string.Format("token={0}", token));

        request.Method = "POST";
        byte[] data = Encoding.UTF8.GetBytes(input);
        request.ContentLength = input.Length;
        request.ContentType = "multipart/form-data";
        Stream newStream = request.GetRequestStream();
        newStream.Write(data, 0, data.Length);
        newStream.Close();
        newStream.Flush();

        var response3 = (HttpWebResponse)request.GetResponse();

        var stream = response3.GetResponseStream();

        StreamReader reader = new StreamReader(stream);
        string result3 = reader.ReadToEnd();