以下是我的服务合同
[OperationContract]
[WebInvoke(Method = "POST", UriTemplate = "/SavePrint?seq={seq}", BodyStyle = WebMessageBodyStyle.Bare)]
bool SavePrint(int seq, string print_lines);
字符串将始终超过3000个字符,因此我无法在Url中传递它。
我尝试使用WebClient类上传。
WebClient client = new WebClient();
client.Headers["Content-type"] = "application/xml";
client.UploadString(baseURLTxtBox.Text + "/SavePrint?seq=1", LongStringHere);
当我尝试使用WebClient类的UploadString方法传递它时,服务失败并显示错误There was an error checking start element of object of type System.String. The data at the root level is invalid. Line 1, position 1.
我假设因为它被期望为XML所以我将我的字符串包装在XML
中<string xmlns="">LongStringHere</string>
然后我收到错误
无法使用根名称'string'和根命名空间''反序列化XML主体(对于操作'SavePrint'和契约('IPrintAPI', 'http://tempuri.org/'))使用DataContractSerializer。确保 对应于XML的类型被添加到已知类型集合中 服务。
我不知道我应该如何在已知类型集合中添加字符串。
答案 0 :(得分:1)
包装"<string></string>"
不是编码xml的正确方法。
并且不确定为什么你用xml编码....如果字符串不是xml。
见
http://www.w3.org/TR/html401/interact/forms.html#form-content-type
不管....尝试
string url = baseURLTxtBox.Text;
using(WebClient client = new WebClient())
{
System.Collections.Specialized.NameValueCollection nvc = new System.Collections.Specialized.NameValueCollection();
nvc.Add("ParameterOne", "!@#$%^&*() Anything Any Characters Here");
nvc.Add("ParameterTwo", LongString);
byte[] responsebytes = client.UploadValues(url, "POST", nvc);
string responsebody = Encoding.UTF8.GetString(responsebytes);
}
APPEND:
[OperationContract]
[WebInvoke(Method = "POST", UriTemplate = "Mod?ParameterOne={ParameterOne}&ParameterTwo={ParameterTwo}")]
bool SavePrint(string ParameterOne, string ParameterTwo);
APPEND:
现在尝试增加你的maxReceivedMessageSize。
<endpoint
address = "http://somethinghere"
binding = "basicHttpBinding" bindingConfiguration="BasicHttpEndpointBinding"
contract = "MyIService" >
</endpoint>
<basicHttpBinding>
<binding name="BasicHttpEndpointBinding"
maxBufferSize="9000000"
maxReceivedMessageSize="9000000" >
<security mode = "None">
</security>
</binding>