简体   繁体   English

通过Web POST将带有特殊字符/符号的字符串发送到PHP服务

[英]Sending a string with special characters/symbols to PHP service through web POST

I'm trying to send a long string through HTTP post using C# and the WebClient . 我正在尝试使用C#和WebClient通过HTTP发布发送长字符串。 This code works when the string does not contain any special characters/symbols, but fails when the string does: 当字符串不包含任何特殊字符/符号时,此代码有效,但当字符串包含任何特殊字符/符号时,此代码失败:

        string stringtosend= File.ReadAllText("string.txt");

        using (WebClient webClient = new WebClient())
        {
            string address = myurl;
            byte[] postData = Encoding.UTF8.GetBytes("texto"= + stringtosend); 
            webClient.Headers[HttpRequestHeader.Accept] = "text/html, application/xhtml+xml, */*";
            webClient.Headers[HttpRequestHeader.AcceptLanguage] = "ru-RU";
            webClient.Headers[HttpRequestHeader.AcceptEncoding] = "gzip, deflate";
            webClient.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded";
            webClient.UploadData(address, postData);
        }

    }

The service the file is being sent to is implemented in PHP. 文件发送到的服务是用PHP实现的。 The PHP code is as follows: PHP代码如下:

//GUARDAR EL LOG
if ($texto_post != "" && $id_post != "" && $tarea_post == "klog") {
    $fp = fopen("klog.txt", 'w');
    fwrite($fp, $texto_post);
    fclose($fp);
    die();

}

I tried to encode with Base64 but it does not work for strings that contains special characters/symbols. 我尝试使用Base64进行编码,但不适用于包含特殊字符/符号的字符串。 The following sample file is around 10kb and fails: https://www.sendspace.com/file/b3i4vl 以下示例文件约为10kb,但失败: https : //www.sendspace.com/file/b3i4vl

When I send a larger file (around 20kb) with only "asdasdasd" repeated, it succeeds. 当我发送一个较大的文件(大约20kb),仅重复“ asdasdasd”时,它成功。 What is causing the file with the special characters to fail? 是什么导致带有特殊字符的文件失败?

There is a limitation on what you can upload by default, see this post: 默认情况下,您可以上传的内容受到限制,请参阅以下文章:

Is there an upload limit when using Web Client in C#? 在C#中使用Web客户端时是否有上传限制?

You can change it in the web.config under the <system.web> section. 您可以在<system.web>部分下的web.config中进行更改。

You are not telling us the server where the services that receive this data is on. 您不是在告诉我们服务器接收此数据的服务所在的位置。

IIS has a limit on how large the data can be. IIS对数据的大小有限制。 Above that limit it will reject. 超过该限制将拒绝。 Apache probably has some limits too. Apache可能也有一些限制。

If it's IIS then change the web.config setting for the <requestLimits> . 如果是IIS,则更改<requestLimits>web.config设置。 The attribute you are looking for is maxAllowedContentLength 您正在寻找的属性是maxAllowedContentLength

You can also do this from the IIS console. 您也可以从IIS控制台执行此操作。 More on this and limits here . 更多关于这一点,并限制在这里

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM