简体   繁体   English

c#WebClient Post返回服务器返回500,但是restclient正在工作

[英]c# WebClient Post returns Server returns 500 but restclient is working

This code returns a 500 此代码返回500

string URL = "http://" + MSSRestSrv + ":8018/Quality/SerialNumbers/BoxBuilds/" + serial + "/Attach/";
string SubAssemblySerialNumber = form.serail;
string Refdes = row.Cells["RefDesc"].Value.ToString().Replace(";", "");

try
{
    using (WebClient client = new WebClient())
    {
        System.Collections.Specialized.NameValueCollection reqparm = new System.Collections.Specialized.NameValueCollection();
        reqparm.Add("SubAssemblySerialNumber", SubAssemblySerialNumber);
        reqparm.Add("Refdes", Refdes);
        byte[] responsebytes = client.UploadValues(URL, "POST", reqparm);
        string responsebody = Encoding.UTF8.GetString(responsebytes);
    }
}
catch (WebException we)
{
    MessageBox.Show(we.Response.ToString());
}

But via Chrome and Advanced Rest Client everything is 200 with this URL as POST 但是通过Chrome和Advanced Rest Client,所有内容都是200,而该网址为POST

http://server:8018/Quality/SerialNumbers/BoxBuilds/999/Attach/?SubAssemblySerialNumber=555&Refdes=SUB1 http:// server:8018 / Quality / SerialNumbers / BoxBuilds / 999 / Attach /?SubAssemblySerialNumber = 555&Refdes = SUB1

Any ideas what's the difference? 有什么想法有什么区别吗?

This is working 这工作

string SubAssemblySerialNumber = form.serail;
string Refdes = row.Cells["RefDesc"].Value.ToString().Replace(";", "");
string URL = "http://" + MSSRestSrv + ":8018/Quality/SerialNumbers/BoxBuilds/" + serial + "/Attach/?SubAssemblySerialNumber=" + SubAssemblySerialNumber + "&Refdes=" + Refdes + "";
try
{
    using (WebClient client = new WebClient())
    {
        System.Collections.Specialized.NameValueCollection reqparm = new System.Collections.Specialized.NameValueCollection();

        byte[] responsebytes = client.UploadValues(URL, "POST", reqparm);
        string responsebody = Encoding.UTF8.GetString(responsebytes);
    }
}
catch (WebException we)
{
    MessageBox.Show(we.Response.ToString());
}

Thanks Martin Parkin 谢谢马丁·帕金

is is working 正在工作

string SubAssemblySerialNumber = form.serail;
string Refdes = row.Cells["RefDesc"].Value.ToString().Replace(";", "");
string URL = "http://" + MSSRestSrv + ":8018/Quality/SerialNumbers/BoxBuilds/" + serial + "/Attach/?SubAssemblySerialNumber=" + SubAssemblySerialNumber + "&Refdes=" + Refdes + "";
try
{
    using (WebClient client = new WebClient())
    {
        System.Collections.Specialized.NameValueCollection reqparm = new System.Collections.Specialized.NameValueCollection();

        byte[] responsebytes = client.UploadValues(URL, "POST", reqparm);
        string responsebody = Encoding.UTF8.GetString(responsebytes);
    }
}
catch (WebException we)
{
    MessageBox.Show(we.Response.ToString());
}
    WebClient wc = new WebClient();
                wc.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
                wc.Encoding = System.Text.Encoding.UTF8;
                wc.UseDefaultCredentials = true;
                string cRresultData = wc.UploadString(URI, "POST", CRreq);
                //string cRresultData = wc.UploadString(URI,CRreq);
                string cRresponse = "";

this code gives me  500 Server error

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

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