سؤال

أرغب في إعادة بناء طلب وظيفة إلى عنوان معين، اتركه على سبيل المثال

http://staging.myproject.com/products.xml؟productitittle§titestest&productcontententless§tecontent&productprepricepless=12.3&tags=aaaaBb.

لطلبات النشر، قمت بإنشاء طريقة عالمية:

private String postMethod(String url, HashMap<String, String> headers, String encodedAuthorizationString) throws HttpException, IOException {
    HttpClient client = new HttpClient();
    PostMethod post = new PostMethod(url);
    post.setRequestHeader("Authorization", encodedAuthorizationString);
    if(headers != null && !headers.isEmpty()){
        for(Entry<String, String> entry : headers.entrySet()){
            post.setRequestHeader(new Header(entry.getKey(), entry.getValue()));
        }
    }
    client.executeMethod(post);
    String responseFromPost = post.getResponseBodyAsString();
    post.releaseConnection();
    return responseFromPost;
}

حيث يمثل الرؤوس أزواج (مفتاح، قيمة)، على سبيل المثال ("المنتج [العنوان]، "العمة الإذاعية"). حاولت استخدام الطريقة عن طريق استدعاء postmethod ("http: //staging.myproject.com.products.xml."، رؤوس" XXX ")؛ حيث شملت رؤوس أزواج

("المنتج [العنوان]، "القمة")،

("المنتج [المحتوى]، "testcontent")،

(المنتج [السعر]، "12.3")،

("العلامات"، "AAA، BBB")

ولكن عاد الخادم رسالة خطأ.

هل يعرف أحد كيفية تحليل العنوان

http://staging.myproject.com/products.xml؟productitittle§titestest&productcontententless§tecontent&productprepricepless=12.3&tags=aaaaBb.

من أجل استخدامه مع الطريقة أعلاه؟ أي جزء هو عنوان URL؟ هي المعلمات تعيين بشكل صحيح؟

شكرا لك.

هل كانت مفيدة؟

المحلول

لقد وجدت مشكلة:

private String postMethod(String url, HashMap<String, String> headers, String encodedAuthorizationString) throws HttpException, IOException {
    HttpClient client = new HttpClient();
    PostMethod post = new PostMethod(url);
    post.setRequestHeader("Authorization", encodedAuthorizationString);
    if(headers != null && !headers.isEmpty()){
        for(Entry<String, String> entry : headers.entrySet()){
            //post.setRequestHeader(new Header(entry.getKey(), entry.getValue()));
            //in the old code parameters were set as headers (the line above is replaced with the line below)
            post.addParameter(new Header(entry.getKey(), entry.getValue()));
        }
    }
    client.executeMethod(post);
    String responseFromPost = post.getResponseBodyAsString();
    post.releaseConnection();
    return responseFromPost;
}

URL = http://staging.myproject.com/products.xml

المعلمات:

("المنتج [العنوان]، "القمة")،

("product[content]", "TestContent"),

(product[price], "12.3"),

("tags", "aaa,bbb")

نصائح أخرى

يبدو أنك ترتدي معلمات استعلام URL، مثل المنتج [السعر] = 12.3 مع رؤوس طلب HTTP. يؤدي استخدام SETREQUESTHEADHEADERER () إلى تعيين رؤوس طلب HTTP، وهي بيانات Meta المرتبطة بأي طلب HTTP.

من أجل ضبط معلمات الاستعلام، يجب عليك إلحاقها بعنوان URL، بعد "؟" وعور urlencoded، كما هو الحال في عنوان URL الخاص بك.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top