0

我正在使用以下代码使用 API 将条目发布到 Wufoo.com。代码中没有错误,并且正在以正确的形式创建一个新条目。唯一的问题是新记录的所有字段都是空白的。任何人都可以检查此代码并告诉我我在这里做错了什么。谢谢你的帮助!!!!

string url = "https://cssoft.wufoo.com/api/v3/forms/registration-form/entries.json";
string postdata = "{\"Field1\":\"Anshuman\",\"Field3\":\"3216549870\":\"Field4\":\"test@new.com\"}";
byte[] byteArray = Encoding.UTF8.GetBytes(postdata);

WebRequest myReq = WebRequest.Create(url);

string username = "3QNW-MXE2-O74M-RUC6";
string password = "mfs802r0uokbfd";
string usernamePassword = username + ":" + password;
CredentialCache mycache = new CredentialCache();
mycache.Add(new Uri(url), "Basic", new NetworkCredential(username, password));
myReq.Credentials = mycache;
myReq.Headers.Add("Authorization", "Basic " + Convert.ToBase64String(new ASCIIEncoding().GetBytes(usernamePassword)));
myReq.ContentType = "multipart/form-data";
myReq.Method = "POST";
myReq.ContentLength = byteArray.Length;
Stream dataStream = myReq.GetRequestStream();
// Write the data to the request stream.
dataStream.Write(byteArray, 0, byteArray.Length);
// Close the Stream object.
dataStream.Close();

WebResponse wr = myReq.GetResponse();
Stream receiveStream = wr.GetResponseStream();
StreamReader reader = new StreamReader(receiveStream, Encoding.UTF8);
string content = reader.ReadToEnd();
Response.Write(content);
4

1 回答 1

2

试试这个代码片段,这应该适合你..

    string url = "https://cssoft.wufoo.com/api/v3/forms/registration-form/entries.json";
    string APIKey = "3QNW-MXE2-O74M-RUC6";
    string Hashcode = "mfs802r0uokbfd";
    string usernamePassword = APIKey + ":" + Hashcode;

    // Prepare web request...
    HttpWebRequest myReq =
          (HttpWebRequest)WebRequest.Create(url);
    CredentialCache mycache = new CredentialCache();
    mycache.Add(new Uri(url), "Basic", new NetworkCredential(APIKey, Hashcode));
    myReq.Credentials = mycache;
    myReq.Headers.Add("Authorization", "Basic " + Convert.ToBase64String(new ASCIIEncoding().GetBytes(usernamePassword)));

    ASCIIEncoding encoding = new ASCIIEncoding();
    string postData = "Field1=Anshuman&Field3=3216549870&Field4=test@new.com";
    byte[] data = encoding.GetBytes(postData);


    myReq.Method = "POST";
    myReq.ContentType = "application/x-www-form-urlencoded";
    myReq.ContentLength = data.Length;
    Stream newStream = myReq.GetRequestStream();

    // Send the data.
    newStream.Write(data, 0, data.Length);
    newStream.Close();

干杯!!这会做你正在努力的工作..

于 2014-11-20T12:30:30.543 回答